我有一個使用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();
}
}
你能發表一些代碼嗎? – GrIsHu 2013-02-25 10:38:57
我添加了一些代碼的帖子:) – 2013-02-25 10:52:01
你有沒有找到解決方案? – 2014-02-06 07:31:07