BaseAdapter 设置 notifyDataSetChanged 无效
来源:互联网 发布:热阻计算软件 编辑:程序博客网 时间:2024/05/22 01:49
额。。。。还没弄完。这里面有些问题。
经常遇到这个问题,令我倍感头疼。。。有时候会觉得莫名其妙。今天遇到了一个例子,意外解决了。
先看下适配器代码。
public class MenuAdapter extends BaseExpandableListAdapter {List<Pair<Integer, Class<? extends Activity>>> list = new ArrayList<Pair<Integer,Class<? extends Activity>>>();List<List<Pair<Integer, Class<? extends Activity>>>> listChild = new ArrayList<List<Pair<Integer, Class<? extends Activity>>>>();private LayoutInflater inflater;/****省略了若干行代码***/private int groupSelected,childSelected;public void setSelection(int groupId,int childId){this.groupSelected = groupId;this.childSelected = childId;notifyDataSetChanged();//我在此处通知了adapter 数据集改变。System.out.println(groupSelected+","+childSelected);}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {convertView= inflater.inflate(R.layout.menu_item_group, null);TextView t = (TextView) convertView.findViewById(R.id.txt);Log.d("", groupPosition+",groupSelected:"+groupSelected);if(groupPosition == groupSelected){convertView.setBackgroundResource(scut.shengchuanglibrary.R.color.bg_menu_item_group_select);}t.setText(list.get(groupPosition).first);return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {convertView= inflater.inflate(R.layout.menu_item_child, null);TextView t = (TextView) convertView.findViewById(R.id.txt);t.setText(listChild.get(groupPosition).get(childPosition).first);Log.d("", groupPosition+","+childPosition+",groupSelected:"+groupSelected+",childSelected:"+childSelected);if(groupPosition == groupSelected&& childPosition == childSelected){convertView.setBackgroundResource(scut.shengchuanglibrary.R.color.bg_menu_item_select);}return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}}
下面是调用setSelection 的代码片段:
private void initExpandListView(View view){final ExpandableListView listView = (ExpandableListView) view.findViewById(R.id.expandableListView1);listView.setChildIndicator(null);listView.setGroupIndicator(null);final MenuAdapter adapter = new MenuAdapter(this);listView.setAdapter(adapter);listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {Pair<Integer, Class<? extends Activity >> pair = (Pair<Integer, Class<? extends Activity>>) listView.getExpandableListAdapter().getChild(groupPosition, childPosition);adapter.setSelection(groupPosition, childPosition); // 在该函数中,调用了notifyDataSetChangedreturn false;}});listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {Pair<Integer, Class<? extends Activity >> pair = (Pair<Integer, Class<? extends Activity>>) listView.getExpandableListAdapter().getGroup(groupPosition);if(pair.first==R.string.menu_exit){android.os.Process.killProcess(android.os.Process.myPid());return false;}adapter.setSelection(groupPosition, 0); // 在该函数中,调用了notifyDataSetChangedif(pair.second == null){return false;}return false; //如果我返回true ,则,不会出现我想要的效果,点击后,item 换个背景颜色。}});}
仔细看一下onGroupClick 的api说明。如果返回为true 则表示已经解决了此次click 事件。既然已经解决了,我想在之后的处理流程中,自然会把所有的操作的结束了。不会再去对整个ListView 进行修改。因为此次点击事件已经成功完成了。而更新数据,应该是在整个ongroupclick 事件完成之后的。
所以其实想想。很多时候,遇到notifyDataSetChanged 无效。或许是因为某些原因,把整个事件派发流给截断了。之后就就不再对listview 进行相关操作。而导致没有调用 getview。 所以遇到这种没有更新数据集的情况,可以去查看是否因为某些原因,listview 停止了事件派发。
- BaseAdapter 设置 notifyDataSetChanged 无效
- BaseAdapter notifyDataSetChanged()
- BaseAdapter notifyDataSetChanged()
- BaseAdapter notifyDataSetChanged()
- BaseAdapter notifyDataSetChanged()
- BaseAdapter导致notifyDataSetChanged()无效的四个原因及处理方法
- BaseAdapter导致notifyDataSetChanged()无效的四个原因及处理方法
- notifyDataSetChanged无效
- BaseAdapter的notifyDataSetChanged方法
- 60BaseAdapter导致notifyDataSetChanged()无效的几个原因及处理方法
- BaseAdapter 的notifyDataSetInvalidated 和notifyDataSetChanged
- Android adapter.notifyDataSetChanged无效
- notifyDataSetChanged无效解决方案
- FragmentPagerAdapter刷新notifyDataSetChanged无效
- adapter的notifyDataSetChanged()无效
- PagerAdapter.notifyDataSetChanged() 无效解决办法
- RecyclerView.notifyDataSetChanged无效
- notifyDataSetChanged无效的解决方法
- android 抽屉效果
- 【孩子分糖】-leecode-OJ
- [每日一题] 11gOCP 1z0-053 :2013-10-5 SQL Access Advisor.........................................23
- myeclipse、MyEclipse注册码自动生成代码
- Md5加密简介
- BaseAdapter 设置 notifyDataSetChanged 无效
- 数字图像基础
- 机房收费系统之防止SQL注入
- 荷兰国旗问题
- HttpClient学习研究---第三章:http状态管理
- 我的Hadoop之路——Win7 Eclipse开发环境搭建
- hdu 4324 - Triangle LOVE
- 谈谈Java开发中遇到的资源文件路径问题(一)
- HttpClient学习研究---第四章:HTTP authenticationHTTP身份验证