This article describes the method of Android programming to dynamically update ListView. Share it for your reference, as follows:
Sometimes we need to modify the generated list, add or modify data. NotifyDataSetChanged() can not refresh the Activity without reflashing the Activity after modifying the array bound to the adapter, and notify the Activity to update the ListView. Today's example is to dynamically update ListView through Handler AsyncTask. From today on, the source code learned will be packaged and uploaded to facilitate students to learn and register an account to download.
layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
ListView list layout:
<?xml version="1.0" encoding="utf-8"?> <TextView android: xmlns:andro android:layout_width="fill_parent" android:layout_height="30px" android:textSize="18sp" > </TextView>
Program code:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; publicclass main extends Activity { /** Called when the activity is first created. */ ListView lv; ArrayAdapter<String> Adapter; ArrayList<String> arr=new ArrayList<String>(); @Override publicvoid onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); lv=(ListView)findViewById(); ("123"); ("234"); ("345"); Adapter =new ArrayAdapter<String>(this,, arr); (Adapter); (lvLis); editItem edit=new editItem(); ("0","Item 1");//Change the first item to "first item" Handler handler=new Handler(); (add,3000);//Delay to execute for 3 seconds } Runnable add=new Runnable(){ @Override publicvoid run() { // TODO Auto-generated method stub ("Add one");//Add one (); } }; class editItem extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... params) { ((params[0]),params[1]); // What params gets an array, params[0] is "0" here, and params[1] is "item 1" //(); //You cannot call () to update the UI after executing the addition because it is not the same thread as the UI //The following onPostExecute method will be called by the UI thread after doBackground is executed returnnull; } @Override protectedvoid onPostExecute(String result) { // TODO Auto-generated method stub (result); (); //Execution is completed, update the UI } } private OnItemClickListener lvLis=new OnItemClickListener(){ @Override publicvoid onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // Triggered when clicking on the entry //arg2 is the position of the item in the point setTitle(((arg2))); } }; }
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android database operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.