2014-12-24 48 views
0

所以我有一個回收站視圖適配器在我的MainActivity中設置。該適配器連接到我稱爲組的自定義對象的數組列表。回收站視圖適配器沒有反映變化

public static RecyclerView.Adapter groupsListAdapter; 
public ArrayList<Group> myGroups; 

//內主要的活性的onCreate

groupsListAdapter = new MyAdapter(myGroups); 

在一個不同的活動,我將其通過的意圖更新此數組列表的內容,第一。

Intent groupCreationIntent = new Intent(MainActivity.this, NewGroupActivity.class); 
     groupCreationIntent.putExtra("groupsList",myGroups); 
     startActivity(groupCreationIntent); 

然後在其他活動中更新它。

private ArrayList<Group> groups; 

//在其他活動的onCreate方法

groups = (ArrayList<Group>) getIntent().getSerializableExtra("groupsList"); 

當回到以前的活動(第二個活動是用於輸入一些信息,並與我的數據庫同步的話)我運行此代碼。

​​

問題是列表適配器沒有反映更改,一旦我返回到主要活動,沒有任何內容出現在我的列表中。我究竟做錯了什麼?

+0

如果數組列表得到填充,那麼在獲得結果後,如果在主要活動上做了notifydataset更改的最後部分,那將會更好。 – Ahmed

+0

你檢查了notifydatasetchange()沒有反映變化的其他職位? – Elltz

回答

1
class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    private List<Group> items; 

    public MyAdapter(List<Group> items) { 
    this.items = items; 
    } 

    // .. your implementation 

    // add and call this 
    public void add(Group item) { 
    this.items.add(item); 
    notifyItemInserted(items.size() - 1); 
    } 
} 

它與您的問題沒有關係,您不應該使用靜態字段進行活動與活動的交流。

相關問題