In the actual development process of Android, you will often encounter modifying the value of a specific item in listview. How can we achieve this?
There are two main methods:
Method 1:When a value in the listview item changes, the data has been reloaded to update the listview.
Although the first method can solve the problem, it is not recommended. This will cause unnecessary resource consumption. When the hardware resources and computing capabilities of mobile terminals are limited, it is obvious that the loss is not worth the candle, and the user experience is not good.
Method 2:The second method is the improvement of the first method. The value of one of the listview items has changed. In fact, the data source of listview has changed. Since the data source has changed, we can modify the data source.
The following describes how to modify a file name in listview:
Step 1: Obtain and clickListviewMediumTEMLocation
// Register the event list. setonitemclicklistener (New onitemclicklistener () {@ override public void onitemclick (adapterview <?> Parent, view, int position, long ID) {// todo auto-generated method stub currentposition = position; // obtain the position of the TEM in the listview }});
Step 2: ClickListviewMediumTEMLocation updateListviewAnd notify the adapter of data changes.
String newfilename = edtfilename. gettext (). tostring (). trim (); file. renameto (new file (file. getparent (), newfilename); // rename // update listviewmap <string, Object> map = listitems. get (currentposition); map. put ("FILENAME", newfilename); // Add the new file name to map to replace the old file name listitems. set (currentposition, MAP); // replace the original mapadapter in listitems. notifydatasetchanged (); // notifies simpleadapter of data changes