[转]notifyDataSetChanged() 动态更新ListView
来源:互联网 发布:php如何防止xss攻击 编辑:程序博客网 时间:2024/05/16 12:08
[转]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 @Override19 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 @Override37 public void run() {38 // TODO Auto-generated method stub39 arr.add("增加一项");//增加一项40 Adapter.notifyDataSetChanged(); 41 } 42 };43 class editItem extends AsyncTask<String,Integer,String>{44 @Override45 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 return null; 52 }53 54 @Override55 protected void onPostExecute(String result) {56 // TODO Auto-generated method stub57 super.onPostExecute(result);58 Adapter.notifyDataSetChanged();59 //执行完毕,更新UI60 }61 62 }63 private OnItemClickListener lvLis=new OnItemClickListener(){64 @Override65 public void 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 }
来源网址:http://www.pocketdigi.com/20100827/75.html
上面可以下载相应的源代码。。。
0 0
- (转)notifyDataSetChanged() 动态更新ListView
- [转]notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- notifyDataSetChanged() 动态更新ListView
- 【Android】使用notifyDataSetChanged()动态更新ListView
- notifyDataSetChanged() 动态更新ListView,知识点比较全
- notifyDataSetChanged() 动态更新ListView android线程延迟
- 九度OJ 题目1075:斐波那契数列
- Android layer-list的属性和使用详解
- 安卓监听ScrollView是否滚动
- c++ primer plus 第七章《编程题7.13.1-求两个数的调和平均数》
- C++学习笔记(一)函数模板与类模板
- [转]notifyDataSetChanged() 动态更新ListView
- PHP 网页调用本地exe程序实例
- hdu 3717 Rescue 二分加队列优化(技巧)
- [leetcode] 263.Ugly Number
- windows/Linux下动态库导出函数小结
- 重要的开源资源及50个c/c++源代码网站
- Java的非访问控制符:static,final,abstract
- BitMap设置图片透明度
- Ubuntu下搭建tftp服务器最简单方法