The ListView responds to the Click Event of the item and refreshes the interface, listviewitem

Source: Internet
Author: User

The ListView responds to the Click Event of the item and refreshes the interface, listviewitem

--- Restore content start ---

Recently encountered this problem in implementing the listview function:

The click event is written in the item of the adapter. I don't know how to update the Click Event of the activity in the listview click event. After a long time, I finally solved the problem. The following are the implementation steps:

1. functions to be implemented: Click the recycle bin to delete the item. The specific layout is shown in figure

2. avtivity encapsulates data into bean objects in the infos list set and sends them to adaper.

1 // query all data from the database. dao is the database operation object. 2 BlackContactsDao dao = new BlackContactsDao (this); 3 List <BlackContactsInfo> infos = dao. queryALL (); 4 // Add the returned data to the list set and send it to the adapter to display 5 mdapter = new BlackContactsItemAdapter (this, infos); 6 blackcontactsLvList. setAdapter (mdapter );

3. After the adaper receives the data, it displays the public class BlackContactsItemAdapter extends BaseAdapter {

List <BlackContactsInfo> infos; Context context; String type; // It is constructed with parameters and transmitted to infos data, with context public BlackContactsItemAdapter (Context context, List <BlackContactsInfo> infos) {this. infos = infos; this. context = context;} @ Override public int getCount () {return infos. size () ;}@ Override public Object getItem (int position) {return null ;}@ Override public long getItemId (int position) {return 0 ;} @ Override public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = null; if (convertView = null) {convertView = View. inflate (context, R. layout. item_blackcontacts_list, null); holder = new ViewHolder (); // find the control holder. ivdele = (ImageView) convertView. findViewById (R. id. blackcontacts_iv_dele); holder. tvPhone = (TextView) convertView. findViewById (R. id. item_blackcontacts_phone); holder. tvType = (TextView) convertView. findViewById (R. id. item_blackcontacts_type); // tags converview convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} final BlackContactsInfo info; info = infos. get (position); switch (info. type) {case 0: type = "phone"; break; case 1: type = "SMS"; break; case 2: type = "Phone + SMS"; break ;}
Holder. tvPhone. setText (info. phone); holder. tvType. setText (type); // set the Click Event holder for the waste bin in the item. ivdele. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// when the recycle bin is clicked, it is based on the number of the item to be clicked, delete BlackContactsDao dao = new BlackContactsDao (context); dao from the database. delete (info. phone); // The following is the core code to update the ui infos. remove (info); notifyDataSetChanged () ;}}); return convertView;} class ViewHolder {ImageView ivdele; TextView tvPhone, tvType ;}}

4. Core code:

// After deleting the data from the database, you must also update the data transmitted to the listview (list set). The infos set will remove the infos object. remove (info); // call yydatasetchanged (); to update data. NotifyDataSetChanged ();

 

 


--- Restore content end ---

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.