Renaming a file: dynamically modifying the component attributes of a specified Item in ListView

Source: Internet
Author: User

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
 

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.