标签:
public class MainActivity extends Activity{
private Button btn1;
private Button btn2;
private Button btn3;
private ListView listView;
private SimpleAdapter adapter;
private DBUtil dbUtil;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn_all);
btn2 = (Button) findViewById(R.id.btn_add);
btn3 = (Button) findViewById(R.id.btn_delete);
listView = (ListView) findViewById(R.id.listView);
dbUtil = new DBUtil();
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideButton(true);
setListView();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideButton(true);
setAddDialog();
}
});
btn3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hideButton(true);
setDeleteDialog();
}
});
}
/**
* 设置弹出删除对话框
*/
private void setDeleteDialog() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_delete);
dialog.setTitle("输入想要删除的货物的编号");
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.CENTER);
dialogWindow.setAttributes(lp);
final EditText cNoEditText = (EditText) dialog.findViewById(R.id.editText1);
Button btnConfirm = (Button) dialog.findViewById(R.id.button1);
Button btnCancel = (Button) dialog.findViewById(R.id.button2);
btnConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dbUtil.deleteCargoInfo(cNoEditText.getText().toString());
dialog.dismiss();
hideButton(false);
Toast.makeText(MainActivity.this, "成功删除数据", Toast.LENGTH_SHORT).show();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
hideButton(false);
}
});
dialog.show();
}
/**
* 设置弹出添加对话框
*/
private void setAddDialog() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_add);
dialog.setTitle("输入添加的货物的信息");
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setGravity(Gravity.CENTER);
dialogWindow.setAttributes(lp);
final EditText cNameEditText = (EditText) dialog.findViewById(R.id.editText1);
final EditText cNumEditText = (EditText) dialog.findViewById(R.id.editText2);
Button btnConfirm = (Button) dialog.findViewById(R.id.button1);
Button btnCancel = (Button) dialog.findViewById(R.id.button2);
btnConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dbUtil.insertCargoInfo(cNameEditText.getText().toString(), cNumEditText.getText().toString());
dialog.dismiss();
hideButton(false);
Toast.makeText(MainActivity.this, "成功添加数据", Toast.LENGTH_SHORT).show();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
hideButton(false);
}
});
dialog.show();
}
/**
* 设置listView
*/
private void setListView() {
listView.setVisibility(View.VISIBLE);
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
list = dbUtil.getAllInfo();
adapter = new SimpleAdapter(
MainActivity.this,
list,
R.layout.adapter_item,
new String[] { "Cno", "Cname", "Cnum" },
new int[] { R.id.txt_Cno, R.id.txt_Cname, R.id.txt_Cnum });
listView.setAdapter(adapter);
}
/**
* 设置button的可见性
*/
private void hideButton(boolean result) {
if (result) {
btn1.setVisibility(View.GONE);
btn2.setVisibility(View.GONE);
btn3.setVisibility(View.GONE);
} else {
btn1.setVisibility(View.VISIBLE);
btn2.setVisibility(View.VISIBLE);
btn3.setVisibility(View.VISIBLE);
}
}
/**
* 返回按钮的重写
*/
@Override
public void onBackPressed()
{
if (listView.getVisibility() == View.VISIBLE) {
listView.setVisibility(View.GONE);
hideButton(false);
}else {
MainActivity.this.finish();
}
}
}
public class HttpConnSoap {
public ArrayList<String> GetWebServre(String methodName, ArrayList<String> Parameters, ArrayList<String> ParValues) {
ArrayList<String> Values = new ArrayList<String>();
//ServerUrl是指webservice的url
//10.0.2.2是让android模拟器访问本地(PC)服务器,不能写成127.0.0.1
//11125是指端口号,即挂载到IIS上的时候开启的端口
//Service1.asmx是指提供服务的页面
String ServerUrl = "http://10.0.2.2:11125/Service1.asmx";
//String soapAction="http://tempuri.org/LongUserId1";
String soapAction = "http://tempuri.org/" + methodName;
//String data = "";
String soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ "<soap:Body />";
String tps, vps, ts;
String mreakString = "";
mreakString = "<" + methodName + " xmlns=\"http://tempuri.org/\">";
for (int i = 0; i < Parameters.size(); i++) {
tps = Parameters.get(i).toString();
//设置该方法的参数为.net webService中的参数名称
vps = ParValues.get(i).toString();
ts = "<" + tps + ">" + vps + "</" + tps + ">";
mreakString = mreakString + ts;
}
mreakString = mreakString + "</" + methodName + ">";
/*
+"<HelloWorld xmlns=\"http://tempuri.org/\">"
+"<x>string11661</x>"
+"<SF1>string111</SF1>"
+ "</HelloWorld>"
*/
String soap2 = "</soap:Envelope>";
String requestData = soap + mreakString + soap2;
//System.out.println(requestData);
try {
URL url = new URL(ServerUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
byte[] bytes = requestData.getBytes("utf-8");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout(6000);// 设置超时时间
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
con.setRequestProperty("SOAPAction", soapAction);
con.setRequestProperty("Content-Length", "" + bytes.length);
OutputStream outStream = con.getOutputStream();
outStream.write(bytes);
outStream.flush();
outStream.close();
InputStream inStream = con.getInputStream();
//data=parser(inStream);
//System.out.print("11");
Values = inputStreamtovaluelist(inStream, methodName);
//System.out.println(Values.size());
return Values;
} catch (Exception e) {
System.out.print("2221");
return null;
}
}
public ArrayList<String> inputStreamtovaluelist(InputStream in, String MonthsName) throws IOException {
StringBuffer out = new StringBuffer();
String s1 = "";
byte[] b = new byte[4096];
ArrayList<String> Values = new ArrayList<String>();
Values.clear();
for (int n; (n = in.read(b)) != -1;) {
s1 = new String(b, 0, n);
out.append(s1);
}
System.out.println(out);
String[] s13 = s1.split("><");
String ifString = MonthsName + "Result";
String TS = "";
String vs = "";
Boolean getValueBoolean = false;
for (int i = 0; i < s13.length; i++) {
TS = s13[i];
System.out.println(TS);
int j, k, l;
j = TS.indexOf(ifString);
k = TS.lastIndexOf(ifString);
if (j >= 0) {
System.out.println(j);
if (getValueBoolean == false) {
getValueBoolean = true;
} else {
}
if ((j >= 0) && (k > j)) {
System.out.println("FFF" + TS.lastIndexOf("/" + ifString));
//System.out.println(TS);
l = ifString.length() + 1;
vs = TS.substring(j + l, k - 2);
//System.out.println("fff"+vs);
Values.add(vs);
System.out.println("退出" + vs);
getValueBoolean = false;
return Values;
}
}
if (TS.lastIndexOf("/" + ifString) >= 0) {
getValueBoolean = false;
return Values;
}
if ((getValueBoolean) && (TS.lastIndexOf("/" + ifString) < 0) && (j < 0)) {
k = TS.length();
//System.out.println(TS);
vs = TS.substring(7, k - 8);
//System.out.println("f"+vs);
Values.add(vs);
}
}
return Values;
}
}
public class DBUtil {
private ArrayList<String> arrayList = new ArrayList<String>();
private ArrayList<String> brrayList = new ArrayList<String>();
private ArrayList<String> crrayList = new ArrayList<String>();
private HttpConnSoap Soap = new HttpConnSoap();
public static Connection getConnection() {
Connection con = null;
try {
//Class.forName("org.gjt.mm.mysql.Driver");
//con=DriverManager.getConnection("jdbc:mysql://192.168.0.106:3306/test?useUnicode=true&characterEncoding=UTF-8","root","initial");
} catch (Exception e) {
//e.printStackTrace();
}
return con;
}
/**
* 获取所有货物的信息
*
* @return
*/
public List<HashMap<String, String>> getAllInfo() {
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
arrayList.clear();
brrayList.clear();
crrayList.clear();
crrayList = Soap.GetWebServre("selectAllCargoInfor", arrayList, brrayList);
HashMap<String, String> tempHash = new HashMap<String, String>();
tempHash.put("Cno", "Cno");
tempHash.put("Cname", "Cname");
tempHash.put("Cnum", "Cnum");
list.add(tempHash);
for (int j = 0; j < crrayList.size(); j += 3) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("Cno", crrayList.get(j));
hashMap.put("Cname", crrayList.get(j + 1));
hashMap.put("Cnum", crrayList.get(j + 2));
list.add(hashMap);
}
return list;
}
/**
* 增加一条货物信息
*
* @return
*/
public void insertCargoInfo(String Cname, String Cnum) {
arrayList.clear();
brrayList.clear();
arrayList.add("Cname");
arrayList.add("Cnum");
brrayList.add(Cname);
brrayList.add(Cnum);
Soap.GetWebServre("insertCargoInfo", arrayList, brrayList);
}
/**
* 删除一条货物信息
*
* @return
*/
public void deleteCargoInfo(String Cno) {
arrayList.clear();
brrayList.clear();
arrayList.add("Cno");
brrayList.add(Cno);
Soap.GetWebServre("deleteCargoInfo", arrayList, brrayList);
}
}
标签:
原文地址:http://my.oschina.net/qiuqidefengjing/blog/425181