如何动态刷新ListView的显示---notifyDataSetChanged
来源:互联网 发布:知乎娱乐八卦爆料 编辑:程序博客网 时间:2024/05/18 12:29
我们在使用ListView的时候,可能会碰到需要动态改变ListView的显示的情况。比如说在ListView后面加一个Button,点击Button后,就重刷ListView将该List Item去掉,怎么实现呢?
可以用notifyDataSetChanged。
简单举例,我们想要列出系统中部分ACTION的Application,然后列举出所有应用后,在List Item后面增加一个按钮,点击该按钮,我们可以去掉该List Item。
1、setupAppLists()
if (mPackMgr == null) {return ;}// create a list to store all custom app infos.mEnableAppInfoList = new ArrayList<DisplayAppInfo>();mDisableAppInfoList = new ArrayList<DisplayAppInfo>();mEnableAppInfoList.clear();mDisableAppInfoList.clear();String[] actions = Actions.getActions();for (String action: actions) {Intent intent = new Intent(action, null);List<ResolveInfo> resolveInfoList = mPackMgr.queryBroadcastReceivers(intent, PackageManager.GET_DISABLED_COMPONENTS);if (resolveInfoList.size() == 0) {continue;}Collections.sort(resolveInfoList, new ResolveInfo.DisplayNameComparator(mPackMgr));for (ResolveInfo resolveInfo: resolveInfoList) {ComponentInfo ci = resolveInfo.activityInfo != null ? resolveInfo.activityInfo : resolveInfo.serviceInfo;ApplicationInfo appInfo = ci.applicationInfo; if (isUserApp(appInfo.flags)) { boolean compState = mPmCompHelper.checkoutComponentEnableSetting(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name); // set display list filter by package name, and fill up the auto start description. setupDisplayListFilterByName(resolveInfo, action, compState);}}}Set<String> keys = mDisplayData.keySet();for (String key : keys) {DisplayAppInfo App = mDisplayData.get(key);if (App.getApplicationStartPermit()) {mEnableAppInfoList.add(App);} else {mDisableAppInfoList.add(App);}}以上大家可以看出来,实际上我是用了两个ListView来显示的,这里和主题关系不大,就不详细讲了。然后我们创建完ListView后呢,肯定会设置对应的adapter。
2、setAdapter
if ((mEnableAppInfoList.size() > 0)|| (mDisableAppInfoList.size() > 0)) {mEnableTitle.setVisibility(View.VISIBLE);mDisableTitle.setVisibility(View.VISIBLE);}mEnableAppListAdapter = new ListViewAdapter(mContext, mEnableAppInfoList, mMsgHandler);mEnableAppListView.setAdapter(mEnableAppListAdapter);mUtil.setListViewHeightBasedOnChildren(mEnableAppListView);mDisableAppListAdapter = new ListViewAdapter(mContext, mDisableAppInfoList, mMsgHandler);mDisableAppListView.setAdapter(mDisableAppListAdapter);mUtil.setListViewHeightBasedOnChildren(mDisableAppListView);
如果我们点击ListView后面的Button按钮,该按钮作用为取消在当前ListView显示,那么我们需要重新将ListView数据做调整,并调用notifyDataSetChanged()。
3、updateListView
private Handler mMsgHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) {case MSG_UPDATE_LIST:{ Bundle bundle = msg.getData(); // get the list item info String pkgName = bundle.getString("pkgname"); int newState = bundle.getInt("newstate"); // update the enable ListView and disable ListView DisplayAppInfo info = mDisplayData.get(pkgName); if (newState == PmEnableStateSetting.BUTTON_STATE_ENABLE) { mDisableAppInfoList.remove(info); ...... info.setApplicationButtonText(getString(R.string.disable)); mEnableAppInfoList.add(info); } else { mEnableAppInfoList.remove(info); ...... info.setApplicationButtonText(getString(R.string.enable)); mDisableAppInfoList.add(info); } mEnableAppListAdapter.notifyDataSetChanged(); mUtil.setListViewHeightBasedOnChildren(mEnableAppListView); mDisableAppListAdapter.notifyDataSetChanged(); mUtil.setListViewHeightBasedOnChildren(mDisableAppListView); }break;default: break;} } };这样ListView列表就重新刷新显示,如何调整ListView的布局参数请参考前一篇博客《ScrollView和ListView冲突问题解决》,敬请关注,谢谢!
- 如何动态刷新ListView的显示---notifyDataSetChanged
- notifyDataSetChanged 动态刷新listview
- listview notifyDataSetChanged()刷新显示
- 关于notifyDataSetChanged不能动态刷新ListView的问题
- 解决用notifyDataSetChanged动态刷新ListView无效的问题
- listview notifydatasetchanged 不刷新的一种解释
- listview notifyDataSetChanged不刷新
- notifyDataSetChanged ListView 不刷新
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- ListView的动态刷新问题——用notifyDataSetChanged没作用
- 【2013-8-12 11:36 星期一 ------ 不求大富大贵,但求问心无愧】
- Secure CRT 自动记录日志 配置 小记
- JavaMail API简介完整介绍
- IE10 下 ASP.NET PageRequestManager、_doPostBack 未定义 解决方法
- oracle系统表v$session、v$sql表的列字段说明
- 如何动态刷新ListView的显示---notifyDataSetChanged
- 破解Xcode实现无证书真机调试(支持Xcode4.6)
- csdn公司下载难
- 网站出现问题时,站长该如何解决
- 今时今日,C还适合当下之所需么?
- 继承ViewGroup研究
- hdu1048map解法和一般解法
- android 关于fm music的操作
- html是怎么回事?我是这样认为的,反正自己知道的不是很多!