notifyDataSetChanged() 动态更新ListView 通过 Handler AsyncTask两种方式

有时候我们需要修改已经生成的列表,添加或者修改数据,notifyDataSetChanged()可以在修改适配器绑定的数组后,不用重新刷新Activity,通知Activity更新ListView。今天的例子就是通过Handler AsyncTask两种方式来动态更新ListView.从今天起,每次学习的源代码都会打包上传,方便各位同学学习,注册帐号即可下载。
布局main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
<?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:

1
2
3
4
5
6
7
8
<?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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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 when the activity is first 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","第1项");//把第一项内容改为"第一项"
    	Handler handler=new Handler();
    	handler.postDelayed(add,3000);//延迟3秒执行
    }
    Runnable add=new Runnable(){
 
		@Override
		public void run() {
			// TODO Auto-generated method stub
			arr.add("增加一项");//增加一项
			Adapter.notifyDataSetChanged();	
		}   	
    };
    class editItem extends AsyncTask<String,Integer,String>{
		@Override
		protected String doInBackground(String... params) {
				arr.set(Integer.parseInt(params[0]),params[1]);
				//params得到的是一个数组,params[0]在这里是"0",params[1]是"第1项"
				//Adapter.notifyDataSetChanged();
				//执行添加后不能调用 Adapter.notifyDataSetChanged()更新UI,因为与UI不是同线程
				//下面的onPostExecute方法会在doBackground执行后由UI线程调用
			return null;	
		}
 
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			Adapter.notifyDataSetChanged();
			//执行完毕,更新UI
		}
 
    }
    private OnItemClickListener lvLis=new OnItemClickListener(){
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			//点击条目时触发
			//arg2即为点中项的位置
			setTitle(String.valueOf(arr.get(arg2)));
 
		}
 
    };
 
}

打包的源代码中有错误,Adapter.notifyDataSetChanged();在doInBackground中,请作相应修改,感谢楼下同学提醒。
下载本例源代码

使用GridView的时候,要求数据逐渐加载,所以使用了adapter.notifyDataSetChanged()方法,

但是发现调用该方法后,点击GridView不起作用了。

找了好长时间,才发现,我是在非UI线程中调用Adapter.notifyDataSetChanged()方法的,

把它放到UI线程之后,就正常了。记下来以便日后参考。


http://www.pocketdigi.com/20100827/75.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值