Android-based Dynamic listview update

Source: Internet
Author: User

This is a transfer post: I personally tested it, the code is indeed available, you can also implement the function, there is a good reference value. Http://www.cnblogs.com/wangjianhui/archive/2011/06/15/2081705.html

Sometimes we need to modify the generated list and add or modify data. yydatasetchanged () can notify the activity to update the listview without refreshing the activity after modifying the array bound to the adapter. In today's example, we use handler asynctask to dynamically update the listview. From today on, the source code for each study will be packaged and uploaded, so that you can easily learn and register an account to download it.
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"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="30px"
android:textSize="18sp"
></TextView>

Program code:

1 import java. util. arraylist;
2
3 Import Android. App. activity;
4 Import Android. OS. asynctask;
5 import Android. OS. Bundle;
6 Import Android. OS. Handler;
7 Import Android. View. view;
8 Import Android. widget. adapterview;
9 Import Android. widget. arrayadapter;
10 Import Android. widget. listview;
11 import Android. widget. adapterview. onitemclicklistener;
12
13 publicclass main extends activity {
14/** called when the activity is first created .*/
15 listview lv;
16 arrayadapter <string> adapter;
17 arraylist <string> arr = new arraylist <string> ();
18 @ override
19 publicvoid oncreate (bundle savedinstancestate ){
20 super. oncreate (savedinstancestate );
21 setcontentview (R. layout. Main );
22 Lv = (listview) findviewbyid (R. Id. lv );
23 arr. Add ("123 ");
24 arr. Add ("234 ");
25 arr. Add ("345 ");
26 adapter = new arrayadapter <string> (this, R. layout. playlist, arr );
27 LV. setadapter (adapter );
28 LV. setonitemclicklistener (lvlis );
29 edititem edit = new edititem ();
30 edit.exe cute ("0", "item 1st"); // change the first item to "item 1"
31 handler = new handler ();
32 handler. postdelayed (ADD, 3000); // execution delayed by 3 seconds
33}
34 runnable add = new runnable (){
35
36 @ override
37 publicvoid run (){
38 // todo auto-generated method stub
39 arr. Add ("add one"); // Add one
40 Adapter. notifydatasetchanged ();
41}
42 };
43 class edititem extends asynctask <string, integer, string> {
44 @ override
45 protected string doinbackground (string... Params ){
46 arr. Set (integer. parseint (Params [0]), Params [1]);
47 // What Params gets is an array. Params [0] is "0" here, And Params [1] is "1st items"
48 // Adapter. notifydatasetchanged ();
49 // you cannot call Adapter. notifydatasetchanged () to update the UI after adding the UI, because it is not in the same thread as the UI.
50 // The following onpostexecute method will be called by the UI thread after dobackground is executed
51 returnnull;
52}
53
54 @ override
55 protectedvoid onpostexecute (string result ){
56 // todo auto-generated method stub
57 super. onpostexecute (result );
58 Adapter. notifydatasetchanged ();
59 // after execution, update the UI
60}
61
62}
63 private onitemclicklistener lvlis = new onitemclicklistener (){
64 @ override
65 publicvoid onitemclick (adapterview <?> Arg0, view arg1, int arg2,
66 long arg3 ){
67 // triggered when an entry is clicked
68 // arg2 is the position of the item in the point
69 settitle (string. valueof (ARR. Get (arg2 )));
70
71}
72
73 };
74
75}
 
Related Article

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.