Android app development-listview long press to delete the item data and delete the records of the database

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.