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

来源:互联网 发布:仿真软件matlab 编辑:程序博客网 时间:2024/06/07 06:04

源网址:http://www.pocketdigi.com/20100827/75.html

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <ListView android:id="@+id/lv"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. </LinearLayout>

ListView列表布局playlist.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <TextView
  3. android:id="@+id/text1"
  4. xmlns:android="http://schemas.android.com/apk/res/android"
  5. android:layout_width="fill_parent"
  6. android:layout_height="30px"
  7. android:textSize="18sp"
  8. ></TextView>

程序代码:

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

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

0 0
原创粉丝点击