Chinaunix首页 | 论坛 | 博客
  • 博客访问: 13030255
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: Android平台

2014-12-19 16:55:43

    使用adpater与listview捆绑后,有时希望在程序使用过程中能动态的更改listview中显示的数据,如何处理?

一、关键代码段


  1. private ListView mListView;
  2.     private Button mBtnChange;
  3.     private android.view.View.OnClickListener mCancelClick;
  4.     private ArrayList<String> mStarNameList = new ArrayList<String>();
  5.     private String[] mAnimalNameList = new String[] {"Dog","Cat","Snake","Duck","Bird"};
  6.     private String[] mStarNameList1 = new String[] {"Kobe","James","Durant","Wade","Love"};
  7.     private ArrayAdapter<String> mAdapter;
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_main);
  12.         this.setCtrlsEventHandler();
  13.         this.mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,mStarNameList);
  14.         this.mListView = (ListView)findViewById(R.id.listView1);
  15.         this.mBtnChange = (Button)findViewById(R.id.button1);
  16.         this.mBtnChange.setOnClickListener(this.mCancelClick);
  17.         this.mStarNameList.add("Kobe");
  18.         this.mStarNameList.add("James");
  19.         this.mStarNameList.add("Durant");
  20.         this.mStarNameList.add("Wade");
  21.         this.mListView.setAdapter(this.mAdapter);
  22.         this.mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  23.     }
  24.     private boolean mIsMutex = false;
  25.     private void setCtrlsEventHandler()
  26.     {
  27.         this.mCancelClick = new View.OnClickListener()
  28.         {
  29.             @Override
  30.             public void onClick(View v)
  31.             {
  32.                 if(mIsMutex)
  33.                 {
  34.                     mBtnChange.setText("True");
  35.                     mStarNameList.clear();
  36.                     for(int i=0;i<mAnimalNameList.length;i++)
  37.                         mStarNameList.add(mAnimalNameList[i]);
  38.                     mAdapter.notifyDataSetChanged();
  39.                     mIsMutex = false;
  40.                 }
  41.                 else
  42.                 {
  43.                     mBtnChange.setText("False");
  44.                     mStarNameList.clear();
  45.                     for(int i=0;i<mStarNameList1.length;i++)
  46.                         mStarNameList.add(mStarNameList1[i]);
  47.                     mAdapter.notifyDataSetChanged();
  48.                     mIsMutex = true;
  49.                 }
  50.             }
  51.         };
  52.     }


功能实现主要原理:

    用ArrayList mStarNameList 给Adapter填充数据,再将listview与adapter捆绑。

    当要修改数据时候,只需要对mStarNameList 进行修改,再调用Adapter.notifyDataSetChanged()就可以实现效果。

 

二、执行效果

image

image



参考文献:

阅读(7120) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~