Setting the ListView content Refresh issue in Android

Source: Internet
Author: User

Recently encountered an issue in learning Android's ListView control, how to add an item to the ListView and refresh it in a timely manner. On the internet to check a lot of posts, many people are asking, but also a lot of people in the answer, but in general did not find a detailed solution. For the ListView to synchronize with the database, the master recommends using the ContentProvider object. But what if I don't use the database? Some people also answer with the notifydatasetchanged () method. This is a correct solution, but for the novice, still more confused how to use, here I put my usage:

Code

Onclicklistener Listener2= NewOnclicklistener () {
@Override
Public voidOnClick (View v) {
//TODO auto-generated Method Stub
Map<String, Object>m= NewHashMap<String, Object>();
String tistring
=Tv.gettext (). toString ();//Notice why I'm taking out this edittext content.
M.put ("Prod_na", tistring); //and not so write M.put ("Prod_na", Tv.gettext ());
M.put ("Prod_type", tistring); //because Tv.gettext () returns the editable object, the real data is cached,
//that means your subsequent edittext changes will affect the item you added earlier.
Coll.add (m);

//retrieves the adapter object that is used to invoke the Notifydatasetchanged method.
Simpleadapter Sadapter=(Simpleadapter) lv.getadapter ();
Sadapter.notifydatasetchanged ();
}
};

Here comes the MVC pattern concept in Android, how to store data, control and display.

You can think of a ListView as a view, so what is mode? Obviously the Simpleadapter object, and what is control? That can only be notifydatasetchanged (). When the data changes, that is, the data processed by simpleadapter changes, then we need to call notifydatasetchanged to notify the view to make a change.

In fact, the code is to set the data to write again!


Setting the ListView content Refresh issue in Android

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.