notifyDataSetChanged
2015-11-24 11:34
225 查看
notifyDataSetChanged 动态更新listview
有时候我们需要修改已经生成的列表,添加或者修改数据,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 public class 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 public void 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 public void run() {
38 // TODO Auto-generated method stub
39 arr.add("增加一项");//增加一项
40 Adapter.notifyDataSetChanged();
41 }
42 };
有时候我们需要修改已经生成的列表,添加或者修改数据,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 public class 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 public void 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 public void run() {
38 // TODO Auto-generated method stub
39 arr.add("增加一项");//增加一项
40 Adapter.notifyDataSetChanged();
41 }
42 };
相关文章推荐
- IntelliJ IDEA 快捷键
- SwipeRefreshLayout 下拉刷新
- ProcessExplorer使用分享
- 面试题(六)
- localtime 和 localtime_r 的区别
- 记录一下接多盟SDK遇到的问题
- 文件及目录操作命令
- Github项目解析(一)-->上传Android项目至github
- springmvc 和struts2的区别
- Leetcode Fraction to Recurring Decimal
- POI实现excel导入
- 进程与线程
- Servlet:下载文件
- 查看文件及内容处理命令
- 查一个表中某个字段相同的其他所有用户信息
- mysql1130远程连接没有权限的解决方法
- pthread_create
- VFL语言简洁
- hbase建索引的两种方式
- HttpClient+List<NameValuePair>发送和接收参数——基于struts2或者ssh等框架