package com.esales.util;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.esales.Guide;
import com.esales.Main;
import com.esales.R;
import com.esales.SelectedItems;
import com.esales.bean.Item;
import com.esales.bean.UserInfo;
import com.esales.db.DataBase;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class Tools {
public static String tips_info;
public static Class back_to_intent;
public static int isSuc;
public static UserInfo user;
public static List<Item> selectedItemsList;
private static DataBase database;
private static SQLiteDatabase dbRead;
private static SQLiteDatabase dbWrite;
public static void showAlertDialog(Context context,String message){
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("出错啦!");
builder.setMessage(message);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.show();
}
/*弹出确认框*/
public static void showConfirmDialog(final Context context,String message,final boolean deleteAll,final Item deleteItem){
if (null==Tools.selectedItemsList||Tools.selectedItemsList.size()==0) {
showAlertDialog(context, "列表已为空!");
}else{
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("提示");
builder.setMessage(message);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if (deleteAll) {
selectedItemsList.removeAll(selectedItemsList);
Intent i=new Intent(context, Guide.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}else{
selectedItemsList.remove(selectedItemsList.indexOf(deleteItem));
if (null==selectedItemsList||0==selectedItemsList.size()) {
selectedItemsList.removeAll(selectedItemsList);
Intent i=new Intent(context, Guide.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}else{
ArrayList<HashMap<String, Object>> listitem=new ArrayList<HashMap<String,Object>>();
int size=Tools.selectedItemsList.size();
for(int i=0;i<size;i++){
HashMap<String, Object> map=new HashMap<String, Object>();
map.put("pno","产品编号:"+Tools.selectedItemsList.get(i).getPno());
map.put("num","数量:"+Tools.selectedItemsList.get(i).getNum());
listitem.add(map);
}
MyAdapter myAdapter=new MyAdapter(
context,
listitem, R.layout.item,
new String[]{"pno","num"},
new int[]{R.id.lbeSelectedItemPno,
R.id.lbeSelectedItemNum});
SelectedItems.selectedItems.setAdapter(myAdapter);
}
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
builder.show();
}
}
/*读取登录用户信息*/
public static UserInfo getUserInfo(Context context){
database=new DataBase(context, "esales");
dbRead=database.getReadableDatabase();
UserInfo user=new UserInfo();
Cursor c=dbRead.query("userInfo", new String[]{"company","username","userpwd"}, null, null, null, null, null);
if (c.moveToNext()) {
user.setCompany(c.getString(c.getColumnIndex("company")));
user.setUsername(c.getString(c.getColumnIndex("username")));
user.setUserpwd(c.getString(c.getColumnIndex("userpwd")));
}
c.close();
dbRead.close();
database.close();
return user;
}
/*保存用户信息*/
public static boolean saveUser(Context context,UserInfo userInfo){
boolean flag=false;
try {
database=new DataBase(context, "esales");
dbWrite=database.getWritableDatabase();
ContentValues values_user=new ContentValues();
values_user.put("company", userInfo.getCompany());
values_user.put("username", userInfo.getUsername());
values_user.put("userpwd", userInfo.getUserpwd());
dbWrite.delete("userInfo", null, null);
dbWrite.insert("userInfo", null, values_user);
flag=true;
} catch (Exception e) {
// TODO Auto-generated catch block
flag=false;
e.printStackTrace();
}finally{
dbWrite.close();
database.close();
}
return flag;
}
/*数据同步后保存数据*/
public static boolean saveProducts(Context context,String productsStr){
boolean flag=false;
try {
database=new DataBase(context, "esales");
dbWrite=database.getWritableDatabase();
String[] arr=productsStr.split(";");
int size_i=arr.length;
if (null!=productsStr&&!"".equals(productsStr)) {
dbWrite.delete("productInfo", null, null);
for (int i = 0; i <size_i; i++) {
String[] arry=arr[i].split(",");
ContentValues values_product=new ContentValues();
values_product.put("pno", arry[0]);
values_product.put("pname", arry[1]);
values_product.put("ptype", arry[2]);
dbWrite.insert("productInfo", null, values_product);
}
}
flag=true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
dbWrite.close();
database.close();
}
return flag;
}
/*读取产品类别列表*/
public static List<String> getProductTypes(Context context){
List<String> data=new ArrayList<String>();
database=new DataBase(context, "esales");
dbRead=database.getReadableDatabase();
Cursor c=dbRead.query("productInfo", new String[]{"ptype"}, null, null, null, null, null);
while (c.moveToNext()) {
data.add(c.getString(c.getColumnIndex("ptype")));
}
Set<String> set=new HashSet<String>(data);
c.close();
dbRead.close();
database.close();
return new ArrayList<String>(set);
}
public static List<String> getProductsByType(Context context,String type){
List<String> data=new ArrayList<String>();
database=new DataBase(context, "esales");
dbRead=database.getReadableDatabase();
String[] arr=new String[]{type};
Cursor c=dbRead.query("productInfo", new String[]{"pno,pname"}, "ptype=?", arr, null, null, null);
while (c.moveToNext()) {
// data.add(c.getString(c.getColumnIndex("pno"))+c.getString(c.getColumnIndex("pname")));
data.add(c.getString(c.getColumnIndex("pno")));
}
c.close();
dbRead.close();
database.close();
return data;
}
public static List<String> getProductsByPno(Context context,String inputPno){
List<String> data=new ArrayList<String>();
data.add("--请选择相关产品--");
database=new DataBase(context, "esales");
dbRead=database.getReadableDatabase();
String[] arr=new String[]{inputPno+"%"};
Cursor c=dbRead.query("productInfo", new String[]{"pno"}, "pno like ?", arr, null, null, null);
while (c.moveToNext()) {
data.add(c.getString(c.getColumnIndex("pno")));
}
c.close();
dbRead.close();
database.close();
return data;
}
public static void addItem(Item item){
if (null==selectedItemsList) {
selectedItemsList=new ArrayList<Item>();
}
int size=selectedItemsList.size();
if (size==0) {
selectedItemsList.add(item);
}else{
boolean isRepeat=false;
for (int i = 0; i < size; i++) {
if(select
- 1
- 2
- 3
- 4
- 5
- 6
前往页