BaseAdapter 设置 notifyDataSetChanged 无效

额。。。。还没弄完。这里面有些问题。

经常遇到这个问题,令我倍感头疼。。。有时候会觉得莫名其妙。今天遇到了一个例子,意外解决了。

先看下适配器代码。

public class MenuAdapter extends BaseExpandableListAdapter {
	
	List>> list = new ArrayList>>();
	List>>> listChild 
	= new ArrayList>>>();
	
	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);
	}



	@Override
	public 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;
	}

	@Override
	public 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;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 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() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Pair> pair = 
						(Pair>) listView.getExpandableListAdapter().getChild(groupPosition, childPosition);
				adapter.setSelection(groupPosition, childPosition);  // 在该函数中,调用了notifyDataSetChanged
				return false;
			}
		});
		listView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
			
			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				Pair> pair = 
						(Pair>) 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);   // 在该函数中,调用了notifyDataSetChanged
				if(pair.second == null){
					return false;
				}
				
				
				return false;  //如果我返回true ,则,不会出现我想要的效果,点击后,item 换个背景颜色。
			}
		});
	}


在以上的代码中,如果在onGroupClickListener 返回true 。则即使我调用了notifyDataSetChanged 也没有任何效果。adapter 不会重新调用 getview 之类的。但是试了下返回false 则adapter 会立刻调用getview更新数据。

仔细看一下onGroupClick 的api说明。如果返回为true 则表示已经解决了此次click 事件。既然已经解决了,我想在之后的处理流程中,自然会把所有的操作的结束了。不会再去对整个ListView 进行修改。因为此次点击事件已经成功完成了。而更新数据,应该是在整个ongroupclick 事件完成之后的。

所以其实想想。很多时候,遇到notifyDataSetChanged 无效。或许是因为某些原因,把整个事件派发流给截断了。之后就就不再对listview 进行相关操作。而导致没有调用 getview。 所以遇到这种没有更新数据集的情况,可以去查看是否因为某些原因,listview 停止了事件派发。



你可能感兴趣的:(java语法学习笔记,Android)