此為轉發帖:經本人親自測試,代碼的確可用,也可以實現功能,有不錯參考價值。http://www.cnblogs.com/wangjianhui/archive/2011/06/15/2081705.html
有時候我們需要修改已經生成的列表,添加或者修改數據,notifyDataSetChanged()可以在修改適配器綁定的數組后,不用重新刷新Activity,通知Activity更新ListView。今天的例子就是通過Handler AsyncTask兩種方式來動態更新ListView.從今天起,每次學習的源代碼都會打包上傳,方便各位同學學習,注冊帳號即可下載。
布局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列表布局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>
程序代碼:
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.execute("0","第1項");//把第一項內容改為"第一項"
31 Handler handler=new Handler();
32 handler.postDelayed(add,3000);//延遲3秒執行
33 }
34 Runnable add=new Runnable(){
35
36 @Override
37 publicvoid run() {
38 // TODO Auto-generated method stub
39 arr.add("增加一項");//增加一項
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 //params得到的是一個數組,params[0]在這里是"0",params[1]是"第1項"
48 //Adapter.notifyDataSetChanged();
49 //執行添加后不能調用 Adapter.notifyDataSetChanged()更新UI,因為與UI不是同線程
50 //下面的onPostExecute方法會在doBackground執行后由UI線程調用
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 //執行完畢,更新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 //點擊條目時觸發
68 //arg2即為點中項的位置
69 setTitle(String.valueOf(arr.get(arg2)));
70
71 }
72
73 };
74
75 }