This article illustrates the method of implementing dynamic update ListView in Android programming. 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 the ListView by handler Asynctask two ways. From today, each study of the source code will be packaged and uploaded to facilitate the students to learn, registered account can be downloaded.
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:
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;
Publicclass Main extends activity {/** called the "when the" is "the" is a-created LV;
Arrayadapter<string> Adapter;
Arraylist<string> arr=new arraylist<string> ();
@Override publicvoid 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 publicvoid run () {//TODO Au
to-generated method Stub Arr.add ("Add one");//Add one adapter.notifydatasetchanged ();
}
}; Class EditItem extends asynctask<string,integer,string>{@Override protected string Doinbackground (String ... p
Arams) {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 Returnnull
; @Override protectedvoid OnPostExecute (String result) {//TODO auto-generated Method stub Super.onpostexecute (re
Sult);
Adapter.notifydatasetchanged (); Execution completed, update UI}} private Onitemclicklistener lvlis=new Onitemclicklistener () {@Override publicvoid onitemcli
CK (adapterview<?> arg0, View arg1, int arg2, Long Arg3) {//Click the entry to trigger the//ARG2 is the position of the item in the Point Settitle (string.valueof (Arr.get (ARG2)));
}
};
}
For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android Database Operating skills summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.