This article illustrates how Android dynamically updates ListView through handler and Asynctask two ways. Share to everyone for your reference, specific as follows:
Sometimes we need to modify the generated list, add or modify the data, notifydatasetchanged () can modify the adapter bound array, do not refresh the activity, notify the Activity update ListView. Today's example is to dynamically update ListView by Handler Asynctask two ways.
Layout main.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent"
>
<listview android:id= "
@+id/lv" android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
</LinearLayout>
ListView List Layout Playlist.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <textview android:id= "@+id/text1"
Http://schemas.android.com/apk/res/android "
android:layout_width=" fill_parent "
android:layout_height=" 30px "
android:textsize=" 18sp
></TextView>
Program code:
Package Com.pocketdigi;
Import java.util.ArrayList;
Import android.app.Activity;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.view.View;
Import Android.widget.AdapterView;
Import Android.widget.ArrayAdapter;
Import Android.widget.ListView;
Import Android.widget.AdapterView.OnItemClickListener;
public class Main extends activity {/** called "the" is "the" is a-created. * * ListView LV;
Arrayadapter<string> Adapter;
Arraylist<string> arr=new arraylist<string> ();
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
lv= (ListView) Findviewbyid (r.id.lv);
Arr.add ("123");
Arr.add ("234");
Arr.add ("345");
Adapter = new Arrayadapter<string> (this,r.layout.playlist, arr);
Lv.setadapter (Adapter);
Lv.setonitemclicklistener (Lvlis);
EditItem edit= New EditItem (); Edit.execute ("0", "1th");//Change the first item to "first" HanDler handler=new handler (); Handler.postdelayed (add,3000)//Delay 3 sec Execution} Runnable add=new Runnable () {@Override public void run () {//TOD
O auto-generated Method Stub Arr.add ("Add one");//Add one adapter.notifydatasetchanged ();
}
}; Class EditItem extends asynctask<string,integer,string>{@Override protected string Doinbackground (string. .
params) {Arr.set (Integer.parseint (Params[0]), params[1]);
Params gets an array, params[0] here is "0", Params[1] is "1th"//adapter.notifydatasetchanged (); The adapter.notifydatasetchanged () Update UI cannot be invoked after the addition is performed because the OnPostExecute method that is not the same thread//below the UI is invoked by the UI thread after Dobackground is executed retur
n null; @Override protected void OnPostExecute (String result) {//TODO auto-generated method stub Super.onp
Ostexecute (result);
Adapter.notifydatasetchanged (); Execution completed, update UI}} private Onitemclicklistener lvlis=new Onitemclicklistener () {@Override public void ONITEMCLick (adapterview<?> arg0, View arg1, int arg2, long arg3) {//Click entry to trigger//ARG2 is the location of the item in the point Setti
Tle (String.valueof (Arr.get (ARG2)));
}
};
}
Packaging source code error, adapter.notifydatasetchanged (), in the Doinbackground, please make the appropriate changes, thank the students to remind.
Full instance code code click here to download the site.
I hope this article will help you with the Android program.