1. Check the listview adapter first.
Mlistitemadapter = new simpleadapter (getapplicationcontext (), mlistitem, // data source R. layout. listviewitem, new string [] {"mtext", "mtime"}, new int [] {R. id. itemtext, R. id. itemtime });
2. Index
Int Index = 0; // the index of the specified data is deleted by long-pressing.
3. Obtain the index by means of a long press.
// Add a long click to obtain the index in the vertex, that is, the parameter arg2mlistview. setonitemlongclicklistener (New onitemlongclicklistener () {@ overridepublic Boolean onitemlongclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stubindex = arg2; return false ;}});
4. Locate the matched item and delete it.
String deletetext = mlistitem. get (index ). get ("mtext "). tostring (); string deletetime = mlistitem. get (index ). get ("mtime "). tostring (); sqlitedatabase DB = dbhelper. getreadabledatabase (); // all the tables are stored in the table, and the _ IDs are arranged in order to facilitate reading data cursor = dB. query ("user", null, null); While (cursor. movetonext () {string mtext = cursor. getstring (cursor. getcolumnindex ("mtext"); // obtain the data string mtime = cursor in the database. getstring (cursor. getcolumnindex ("mtime"); currenttime = new date (); // you can use the primary key value to determine the value of _ idif (deletetext. equals (mtext) & deletetime. equals (cursortime) {deletedata (cursor. getint (0); // Delete the records in the matched database, cursor. getint (0) is the first example for getting the cursor pair and _ idbreak;} dB. close ();
5. deletedata (INT)
Public void deletedata (INT index) {sqlitedatabase DB = dbhelper. getreadabledatabase (); // delete all table data // dB. delete ("users", null, null); // delete a specified piece of data from the table db.exe csql ("delete from" + "user" + "where _ id =" + integer. tostring (INDEX); dB. close ();}
Additional: Use thread to update data. If you do not clear the original data first, the same data will be added to the back of the listview, so you can call
// Clear the original data and update mlistitemadaptermlistitem. Clear (); mlistitemadapter. policydatasetchanged ();
Then read the database again and add it to mlistitem.