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