添加数据时刷新可扩展列表视图:保持状态

4

我使用了一个自定义适配器通过 BaseExpandableListAdapter 填充了一个可扩展的列表 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();
    }

}

你能贴一些代码吗? - GrIsHu
我在帖子中添加了一些代码 :) - PX Developer
你找到解决方案了吗? - Kalpesh Lakhani
@KalpeshLakhani 发布了解决方案。 - Ifrit
1个回答

2
ExpandableListView 无法正确重新展开,因为您没有使用稳定的 ID。使用稳定的 ID 将为您解决问题。
虽然您已经通过以下方式启用了稳定的 ID:
public boolean hasStableIds() {
    return true;
}

你仍需要使用getGroupId()getChildId()方法返回稳定的ID。仅返回groupPositionchildPosition无法作为稳定的ID。
稳定的ID意味着返回的值在整个数据集中是唯一的。此外,无论给定项的位置如何,返回的值都将相同。例如,假设你正在存储人员信息。每个人都有一个唯一的社会安全号码(SSN)。这将是使用getChildId()返回的优秀稳定ID。例如,如果John Smith在位置2、4(组、子项),然后移动到位置(3、1)...他的稳定ID(或SSN)仍将保持不变。仅返回位置数字无法保证这一点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接