У меня есть ExpandableListView
, заполненный с использованием пользовательского адаптера с использованием BaseExpandableListAdapter
. Я могу добавить новые данные в новое действие, а затем мне нужно обновить список. Когда список обновляется, я хочу, чтобы те же группы были расширены.Refresh ExpandableListView при добавлении данных: сохранить состояние
Новые записи могут быть добавлены в любой точке списка. Мы можем добавлять новые группы или только новые дети.
Сейчас я использую:
adapter.notifyDataSetChanged();
Список обновляется, но состояние не то же самое. В примере, если у меня есть три группы: G1 расширен, G2 не расширен, расширен G3, и я добавляю новую группу G4 между G2 и G3, тогда G4 получает состояние G3, так как занимает свою позицию. Есть ли способ сохранить состояние автоматически или мне нужно сделать это вручную в моем адаптере?
Спасибо!
[EDIT]
Добавление кода.
Это мой адаптер:
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
Я добавил код в сообщение :) –
У вас есть решение для этого? –