notifydatasetchanged () Dynamic Update listview

Source: Internet
Author: User

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 the handler Asynctask two ways to dynamically update the ListView. From today onwards, each study of the source code will be packed and uploaded, to facilitate the students to learn, registered account can be downloaded.
Layout main.xml:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"    ><ListViewAndroid: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"   xmlns:android= "http://schemas.android.com/apk/res/android"  Android:layout_ Width= "Fill_parent"  android:layout_height= "30px"  Android : textSize= "18sp"></TextView>

Program code:

Importjava.util.ArrayList; Importandroid.app.Activity;ImportAndroid.os.AsyncTask;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.AdapterView.OnItemClickListener; Public classMainextendsActivity {/**Called when the activity is first created.*/ListView LV; Arrayadapter<String>Adapter; ArrayList<String> arr=NewArraylist<string>(); @Override Public voidonCreate (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=NewArrayadapter<string> ( This, R.layout.playlist, arr);        Lv.setadapter (Adapter);         Lv.setonitemclicklistener (Lvlis); EditItem Edit=NewEditItem (); Edit.execute ("0", "1th item");//change the first item to "first item"Handler handler=NewHandler (); Handler.postdelayed (Add,3000);//3 seconds Delay execution} Runnable Add=NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubArr.add ("Add an item");//Add one itemadapter.notifydatasetchanged ();    }           }; classEditItemextendsAsynctask<string,integer,string>{@Overrideprotectedstring Doinbackground (String ... params) {Arr.set (Integer.parseint (params[0]), params[1]); //The params gets an array, params[0] here is "0", Params[1] is "1th "//adapter.notifydatasetchanged (); //cannot call adapter.notifydatasetchanged () Update UI after execution because it is not the same thread as the UI//The following OnPostExecute method is called by the UI thread after Dobackground executes            return NULL; } @Overrideprotected voidOnPostExecute (String result) {//TODO auto-generated Method Stub            Super. OnPostExecute (Result);            Adapter.notifydatasetchanged (); //after execution, update UI        }     }    PrivateOnitemclicklistener lvlis=NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> arg0, View arg1,intArg2,LongArg3) {            //trigger when clicking on an entry//Arg2 is the position of the item in the pointSettitle (string.valueof (Arr.get (ARG2))); }     }; }

Source URL: http://www.pocketdigi.com/20100827/75.html

The above can download the corresponding source code ...

notifydatasetchanged () Dynamic Update listview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.