第一步:写一个SqliteDBHelper类
1 package com.example.gzznproject; 2 3 import android.content.ContentValues; 4 import android.content.Context; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 import android.database.sqlite.SQLiteOpenHelper; 8 9 public class SqliteDBHelper extends SQLiteOpenHelper{ 10 public static final String COLUMN_ID ="ID"; 11 public static final String COLUMN_ConstrName ="ConstrName"; 12 public static final String COLUMN_Enginer ="Enginer"; 13 public static final String COLUMN_DepartID ="DepartID"; 14 public static final String COLUMN_EnginerName ="EnginerName"; 15 public static final String COLUMN_EnginerAddress ="EnginerAddress"; 16 public static final String COLUMN_WorkContent ="WorkContent"; 17 public static final String COLUMN_WorkRemark ="WorkRemark"; 18 public static final String COLUMN_TodayPlan ="TodayPlan"; 19 public static final String COLUMN_TorrowPlan ="TorrowPlan"; 20 public static final String COLUMN_Remark ="Remark"; 21 public static final String COLUMN_WorkTime ="WorkTime"; 22 public static final String COLUMN_DeletionStateCode ="DeletionStateCode"; 23 public static final String COLUMN_Enabled ="Enabled"; 24 public static final String COLUMN_SortCode ="SortCode"; 25 public static final String COLUMN_Description ="Description"; 26 public static final String COLUMN_CreateOn ="CreateOn"; 27 public static final String COLUMN_CreateUserId ="CreateUserId"; 28 public static final String COLUMN_CreateBy ="CreateBy"; 29 public static final String COLUMN_ModifiedOn ="ModifiedOn"; 30 public static final String COLUMN_ModifiedUserId ="ModifiedUserId"; 31 public static final String COLUMN_ModifiedBy ="ModifiedBy"; 32 public static final String COLUMN_Crsign ="Crsign"; 33 public static final String COLUMN_Telphone ="Telphone"; 34 public static final String COLUMN_Departname ="Departname"; 35 public static final String COLUMN_Itemid ="Itemid"; 36 public static final String COLUMN_Itemname ="Itemname"; 37 public static final String DATABASE_NAME = "TestDB.db"; 38 public static final String USERS_TABLE = "myLog"; 39 40 public static final String ADMIN_USERID="Id"; 41 public static final String ADMIN_USERNAME="userName"; 42 public static final String ADMIN_USERPWD="userPwd"; 43 public static final String ADMIN_USERADDRESS="address"; 44 45 public static final String TABLE_NAME="CREATE TABLE "+ USERS_TABLE +"("+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 46 +COLUMN_ConstrName+" text, "+COLUMN_Enginer+" text, "+COLUMN_DepartID+" text,"+COLUMN_EnginerName+" text,"+COLUMN_EnginerAddress+" text, " 47 +COLUMN_WorkContent+" text, "+COLUMN_WorkRemark+" text, "+COLUMN_TodayPlan+" text,"+COLUMN_TorrowPlan+" text, "+COLUMN_Remark+" text," 48 +COLUMN_WorkTime+" text, "+COLUMN_DeletionStateCode+" text, "+COLUMN_Enabled+" text,"+COLUMN_SortCode+" text,"+COLUMN_Description+" text, " 49 +COLUMN_CreateOn+" text,"+COLUMN_CreateUserId+" text, "+COLUMN_CreateBy+" text, "+COLUMN_ModifiedOn+" text, "+COLUMN_ModifiedUserId+" text, " 50 +COLUMN_ModifiedBy+" text, "+COLUMN_Crsign+" text, "+COLUMN_Telphone+" text, "+COLUMN_Departname+" text, "+COLUMN_Itemid+" text, " 51 +COLUMN_Itemname+" text);"; 52 53 private SQLiteDatabase db; 54 public SqliteDBHelper(Context c) { 55 super(c, DATABASE_NAME,null,1); 56 // TODO Auto-generated constructor stub 57 } 58 @Override 59 public void onCreate(SQLiteDatabase db) { 60 // TODO Auto-generated method stub 61 this.db=db; 62 db.execSQL(TABLE_NAME); 63 } 64 public long Insert(ContentValues values) 65 { 66 SQLiteDatabase db=this.getWritableDatabase(); 67 long count= db.insert(USERS_TABLE, null, values); 68 if(count==-1) 69 { 70 count=-1; 71 } 72 else 73 { 74 count=1; 75 } 76 db.close(); 77 return count; 78 } 79 80 public Cursor query() 81 { 82 SQLiteDatabase db=this.getWritableDatabase(); 83 Cursor c=db.query(USERS_TABLE, null, null, null, null, null, null); 84 return c; 85 } 86 public void Delete(int id) 87 { 88 if(db==null) 89 db=this.getWritableDatabase(); 90 db.execSQL("delete from myLog where ID="+id); 91 db.close(); 92 } 93 94 public void update(int id,ContentValues values) 95 { 96 SQLiteDatabase db = this.getWritableDatabase(); 97 String where = COLUMN_ID + " = ?"; 98 String[] whereValue = { Integer.toString(id) }; 99 db.update(USERS_TABLE, values, where, whereValue); 100 } 101 102 public void Close() 103 { 104 if(db!=null) 105 { 106 db.close(); 107 } 108 } 109 @Override 110 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 111 // TODO Auto-generated method stub 112 113 } 114 }
第二步:列表前台页面展示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="150dp" android:textSize="15sp" android:layout_height="30dp" android:gravity="center_vertical|left" android:text="@string/txt_Name"/> <TextView android:layout_width="160dp" android:textSize="15sp" android:gravity="center_vertical|right" android:layout_height="30dp" android:text="@string/txt_wokTime"/> </LinearLayout> <ListView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listView"/> </LinearLayout>
注意:还需要建一个item.xml文件(item文件没有后台代码)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="horizontal" > 6 <TextView 7 android:layout_width="150dp" 8 android:gravity="center_vertical|left" 9 android:textSize="15sp" 10 android:layout_height="30dp" 11 android:id="@+id/txt_ConstrName" 12 /> 13 <TextView 14 android:layout_width="160dp" 15 android:gravity="center_vertical|right" 16 android:textSize="15sp" 17 android:layout_height="30dp" 18 android:id="@+id/txt_WorkTime" 19 /> 20 21 </LinearLayout>
第三步:展示loglist后台代码
package com.example.gzznproject; import java.util.ArrayList; import java.util.HashMap; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.AndroidHttpTransport; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.ConnectivityManager; import android.os.Bundle; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnCreateContextMenuListener; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class LogListActivity extends Activity { SimpleAdapter adapter; SqliteDBHelper helper; public ListView listview; public HashMap<String, Object> hashmap; public static ArrayList<HashMap<String, Object>> arrayList; public static int enegerid; public static int flagid; public static int userid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loglist); _getdataview(); listview.setOnCreateContextMenuListener(listener); } // listview数据绑定 public void _getdataview() { listview = (ListView) findViewById(R.id.listView); adapter = new SimpleAdapter(LogListActivity.this, getdata(), R.layout.item, new String[] { "ConstrName", "WorkTime" }, new int[] { R.id.txt_ConstrName, R.id.txt_WorkTime }); listview.setAdapter(adapter); } // 长按菜单 OnCreateContextMenuListener listener = new OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("操作提示"); menu.add(0, 2, 0, "文件上传"); menu.add(0, 0, 0, "编辑"); menu.add(0, 1, 0, "删除"); } }; @Override public boolean onContextItemSelected(MenuItem item) { final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); hashmap = arrayList.get((int) info.id); Object obj = hashmap.get("ID"); int id = Integer.parseInt(String.valueOf(obj)); if (item.getItemId() == 1) { if (id == -1) { Toast.makeText(getApplicationContext(), "没有选中数据!", Toast.LENGTH_SHORT).show(); } else { helper.Delete(id); _getdataview(); Toast.makeText(getApplicationContext(), "数据删除成功!", Toast.LENGTH_SHORT).show(); } } else if (item.getItemId() == 0) { if (id == -1) { Toast.makeText(getApplicationContext(), "没有选中数据!", Toast.LENGTH_SHORT).show(); } else { LogManagerActivity.hashmap = hashmap; Intent intent = new Intent(); intent.setClass(LogListActivity.this, LogManagerActivity.class); startActivity(intent); } } else if (item.getItemId() == 2) { String url = "http://117.34.91.186:8347/WebServices/InsertLog.asmx"; String nameSpace = "http://tempuri.org/"; String SOAP_ACTION = "http://tempuri.org/InserttionLog"; String method = "InserttionLog"; SoapObject request = new SoapObject(nameSpace, method); request.addProperty("Id", userid); request.addProperty("ConstrName", hashmap.get("ConstrName") .toString() == null ? "" : hashmap.get("ConstrName") .toString()); request.addProperty("Enginer", hashmap.get("Enginer").toString() == null ? "" : hashmap .get("Enginer").toString()); request.addProperty( "DepartID", hashmap.get("ID").toString() == null ? "" : hashmap.get( "ID").toString()); request.addProperty("EngineName", hashmap.get("EnginerName") .toString() == null ? "" : hashmap.get("EnginerName") .toString()); request.addProperty("EnginerAddress", hashmap.get("EnginerAddress") .toString() == null ? "" : hashmap.get("EnginerAddress") .toString()); request.addProperty("WorkContent", hashmap.get("WorkContent") .toString() == null ? "" : hashmap.get("WorkContent") .toString()); request.addProperty("WorkRemark", hashmap.get("WorkRemark") .toString() == null ? "" : hashmap.get("WorkRemark") .toString()); request.addProperty("TodayPlan", hashmap.get("TodayPlan") .toString() == null ? "" : hashmap.get("TodayPlan") .toString()); request.addProperty("TorrowPlan", hashmap.get("TorrowPlan") .toString() == null ? "" : hashmap.get("TorrowPlan") .toString()); request.addProperty("Remark", hashmap.get("Remark").toString() == null ? "" : hashmap .get("Remark").toString()); request.addProperty("WorkTime", hashmap.get("WorkTime").toString() == null ? "" : hashmap .get("WorkTime").toString()); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = request; envelope.dotNet = true; AndroidHttpTransport transport = new AndroidHttpTransport(url); try { if (isOpenNetwork()) { transport.call(SOAP_ACTION, envelope); if (envelope.getResponse() != null) { Object object = envelope.getResponse(); boolean result = Boolean .parseBoolean(object.toString()); if (result == true) { Toast.makeText(getApplicationContext(), "上传成功!", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "上传失败!", Toast.LENGTH_SHORT).show(); } } } else { Toast.makeText(getApplicationContext(), "当前网络不可用不能上传文件!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } else { return false; } return super.onContextItemSelected(item); } // 绑定listview的数据源 public ArrayList<HashMap<String, Object>> getdata() { helper = new SqliteDBHelper(this); SQLiteDatabase mdb = helper.getReadableDatabase(); Cursor c = mdb.rawQuery("select * from myLog", null); arrayList = new ArrayList<HashMap<String, Object>>(); while (c.moveToNext()) { hashmap = new HashMap<String, Object>(); hashmap.put("ID", c.getString(0)); hashmap.put("ConstrName", c.getString(1)); hashmap.put("Enginer", c.getString(2)); hashmap.put("DepartID", c.getString(3)); hashmap.put("EnginerName", c.getString(4)); hashmap.put("EnginerAddress", c.getString(5)); hashmap.put("WorkContent", c.getString(6)); hashmap.put("WorkRemark", c.getString(7)); hashmap.put("TodayPlan", c.getString(8)); hashmap.put("TorrowPlan", c.getString(9)); hashmap.put("Remark", c.getString(10)); hashmap.put("WorkTime", c.getString(11)); hashmap.put("DeletionStateCode", c.getString(12)); hashmap.put("Enabled", c.getString(13)); hashmap.put("SortCode", c.getString(14)); hashmap.put("Description", c.getString(15)); hashmap.put("CreateOn", c.getString(16)); hashmap.put("CreateUserId", c.getString(17)); hashmap.put("CreateBy", c.getString(18)); hashmap.put("ModifiedOn", c.getString(19)); hashmap.put("ModifiedUserId", c.getString(20)); hashmap.put("ModifiedBy", c.getString(21)); hashmap.put("Crsign", c.getString(22)); hashmap.put("Telphone", c.getString(23)); hashmap.put("Departname", c.getString(24)); hashmap.put("Itemid", c.getString(25)); hashmap.put("Itemname", c.getString(26)); arrayList.add(hashmap); } return arrayList; } @Override public boolean onCreateOptionsMenu(Menu menu) { // getMenuInflater().inflate(R.menu.main, menu); menu.add(0, 1, 1, "添加数据"); menu.add(0, 2, 1, "树形结构"); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == 1) { Intent itent = new Intent(); itent.setClass(LogListActivity.this, LogManagerActivity.class); startActivity(itent); } else { Intent itent = new Intent(); itent.setClass(LogListActivity.this, TreeStructure.class); startActivity(itent); } return false; } // 监听网络状态 private boolean isOpenNetwork() { ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); if (connManager.getActiveNetworkInfo() != null) { return connManager.getActiveNetworkInfo().isAvailable(); } return false; } }
这里还要注意需要在libs文件夹中导入:ksoap2-android-assembly-2.3-jar-with-dependencies.jar 包
还要在AndroidManifest.xml中加入这些权限:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
大功告成,Listview数据绑定成功!