Recently I encountered a problem when learning the listview control of Android. How to add an item to the listview and refresh it in time. I have checked many posts on the Internet, and many people are asking and answering questions, but in general I have not found a detailed solution. For the synchronization between listview and the database, experts suggest using the contentprovider object. But what if I don't use a database? Someone also replied with the yydatasetchanged () method. This is a correct solution, but it is still confusing for new users. Here I will post my usage:
Code Onclicklistener listener2 = New Onclicklistener (){
@ Override
Public Void Onclick (view v ){
// Todo auto-generated method stub
Map < String, Object > M = New Hashmap < String, Object > ();
String tistring = TV. gettext (). tostring (); // Note: Why should I retrieve the edittext content here?
M. Put ( " Prod_na " , Tistring ); // Write M. Put ("prod_na", TV. gettext ());
M. Put ( " Prod_type " , Tistring ); // Because TV. gettext () returns an editable object, the real data is cached,
// That is to say, your subsequent edittext changes will affect the previously added item.
Coll. Add (m );
// Retrieves the adapter object, which is used to call the yydatasetchanged method.
Simpleadapter sadapter = (Simpleadapter) LV. getadapter ();
Sadapter. notifydatasetchanged ();
}
};
Here we will discuss the MVC pattern concept in Android, how to store data, control and display.
You can think that listview is a view. What is the mode? It is obviously a simpleadapter object, and what is control? It can only be yydatasetchanged. When the data changes, that is, the data processed by simpleadapter changes, we need to call yydatasetchanged to notify the view to make a change.