2013-02-25 73 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)仍然是相同的。只返回職位號碼不能保證這一點。

相關問題