Renaming a file: dynamically modifying the component attributes of a specified Item in ListView
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 the value of one of the ListView items changes, reload the data 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 the position of the tem in the ListView
// Register the event list. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView
Parent, View view, int position, long id) {// TODO Auto-generated method stub currentPosition = position; // obtain the position of the tem in the ListView }});
Step 2: update the ListView data source based on the position of the tem in the ListView and notify the adapter of data changes.
String newFileName = edtFileName. getText (). toString (). trim (); file. renameTo (new File (file. getParent (), newFileName); // rename // update ListViewMap
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