2013-02-25 71 views
4

我有一个使用BaseExpandableListAdapter的自定义适配器填充的ExpandableListView。我可以在新活动中添加新数据,然后我必须刷新列表。当清单刷新时,我想保持相同的组展开。添加数据时刷新ExpandableListView:保持状态

可以在列表中的任意位置添加新条目。我们可以添加新的小组或仅添加新的小孩。

现在我使用:

adapter.notifyDataSetChanged(); 

列表将刷新,但国家是不一样的。例如,如果我有三组:G1展开,G2未展开,G3展开,并且我在G2和G3之间添加了一个新的组G4,则G4从其位置开始获取G3状态。有没有办法自动保持状态,或者我必须在我的适配器中手动执行?

谢谢!

[编辑]

添加一些代码。

这是我的适配器:

public class StopsInnerExpandableListAdapter extends BaseExpandableListAdapter { 

private ArrayList<String> groups; // Dates. 
private ArrayList<ArrayList<HashMap<String, String>>> children; // HashMap has stop ID, image and price. 
private Context context; 

public StopsInnerExpandableListAdapter (Context ctx, ArrayList<String> groups, 
     ArrayList<ArrayList<HashMap<String, String>>> children) { 
    context = ctx; 
    this.groups = groups; 
    this.children = children; 
} 

@Override 
public boolean areAllItemsEnabled() { 
    return true; 
} 

public HashMap<String, String> getChild(int groupPosition, int childPosition) { 
    return children.get(groupPosition).get(childPosition); 
} 

public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     // Inflate child's view. 
     LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.stop_row, null); 
    } 

    // Get the stop data and use it to fill the child's views. 
    HashMap<String, String> child = children.get(groupPosition).get(childPosition); 

    ... 

    return convertView; 
} 

public int getChildrenCount(int groupPosition) { 
    return children.get(groupPosition).size(); 
} 

public String getGroup(int groupPosition) { 
    return groups.get(groupPosition); 
} 

public int getGroupCount() { 
    return groups.size(); 
} 

public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     // Inflate group's view. 
     LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.stop_subgroup, null); 
    } 

    String date = groups.get(groupPosition); 

    TextView dateView = (TextView) convertView.findViewById(R.id.title_date); 
    dateView.setText(date); 

    return convertView; 
} 

public boolean hasStableIds() { 
    return true; 
} 

public boolean isChildSelectable(int arg0, int arg1) { 
    return true; 
} 

public void updateData(ArrayList<String> groups, 
     ArrayList<ArrayList<HashMap<String, String>>> children) { 
    this.groups = groups; 
    this.children = children; 
} 
} 

在我的活动我这样做:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.stop_list); 

    mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list); 

    model = new MyModel(this); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    ListEntries entries = model.getEntries(); 

    if(adapter == null){ 
     // Sets the adapter that provides data to the list. 
     adapter = new StopsInnerExpandableListAdapter(this, entries.getDates(), entries.getChilds()); 
     mExpandableList.setAdapter(adapter); 
    } 
    else{ 
     adapter.updateData(entries.getDates(), entries.getChilds()); 
     adapter.notifyDataSetChanged(); 
    } 

} 
+0

你能发表一些代码吗? – GrIsHu 2013-02-25 10:38:57

+0

我添加了一些代码的帖子:) – 2013-02-25 10:52:01

+0

你有没有找到解决方案? – 2014-02-06 07:31:07

回答

2

ExpandableListView不是重新展开,因为你不使用稳定的ID正确。使用稳定的ID将为您解决问题。

虽然你是能够经由这里稳定的ID:

public boolean hasStableIds() { 
    return true; 
} 

你仍然需要实际与getGroupId()getChildId()方法返回稳定标识。分别返回groupPositionchildPosition不符合稳定标识。

要成为一个稳定的ID意味着返回的值在整个数据集中将是唯一的。此外,不管给定物品的位置如何,返回的值都是相同的。例如,假设你正在存储人员。每个人都有一个独特的SSN。这将是一个很好的稳定ID,用getChildId()返回。例如,如果John Smith在位置2,4(组,小孩),然后移动到位置(3,1)......他的稳定ID(或SSN)仍然是相同的。只返回职位号码不能保证这一点。

相关问题