File rename: dynamically modifying the component attributes in the specified Item in ListView, listviewitem

Source: Internet
Author: User

File rename: dynamically modifying the component attributes in the specified Item in ListView, listviewitem

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 <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


How to dynamically change the Item data in ListView?

Listview is a very common control in projects. It's time to study. The answer already exists. Why the source code? As a programmer, this line of thinking is fast. Tailism is not supported.
 
In Android, Listview gets the id of each item and dynamically changes the display on the item

Call the following code when loading and updating data:
// Generate the element corresponding to the Item and dynamic array of the adapter
SimpleAdapter listItemAdapter = new SimpleAdapter (this, listItem, // Data Source
R. layout. listview_orderitems, // XML Implementation of ListItem
// Subitem of the dynamic array and ImageItem
New String [] {"ordercode", "username", "userphone "},
// An ImageView in the XML file of ImageItem, two TextView IDS
New int [] {R. id. ItemTitle, R. id. ItemText, R. id. ItemText1 });
ListItemAdapter. notifyDataSetChanged (); // This statement refreshes the list in time.
// Add and display
List. setAdapter (listItemAdapter );

Related Article

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.