SoFunction
Updated on 2025-04-10

Android dynamically updates ListView through Handler and AsyncTask (with source code)

This article describes the method of Android dynamically updating ListView through Handler and AsyncTask. 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.

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:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class main extends Activity {
 /** Called when the activity is first created. */
  ListView lv;
  ArrayAdapter&lt;String&gt; Adapter;
  ArrayList&lt;String&gt; arr=new ArrayList&lt;String&gt;();
 @Override
 public void onCreate(Bundle savedInstanceState) {
  (savedInstanceState);
  setContentView();
  lv=(ListView)findViewById();
   ("123");
   ("234");
   ("345");
   Adapter = new ArrayAdapter&lt;String&gt;(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
    public void run() {
      // TODO Auto-generated method stub
      ("Add one");//Add one      ();
    }   
 };
 class editItem extends AsyncTask&lt;String,Integer,String&gt;{
    @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      return null;
    }
    @Override
    protected void onPostExecute(String result) {
      // TODO Auto-generated method stub
      (result);
      ();
      //Execution is completed, update the UI    }
 }
 private OnItemClickListener lvLis=new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView&lt;?&gt; 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)));
    }
 };
}

There is an error in the packaged source code, (); in doInBackground, please make corresponding modifications, thanks for the reminder.

Click here for the full instance code codeDownload this site

I hope this article will be helpful to everyone's Android programming design.