2016-04-15 85 views
0

如何從片段中清除ArrayList並執行notifyDataSetChanged()適配器ListView清除ArrayList並在片段的適配器ListView中執行notifyDataSetChanged

這是我的快捷代碼:

public class ConnectionsFragment { 
    public onClick() { 
     // do clear data and notify it (from ListView), how?! 
    } 

    private class ArrayAdapter extends BaseAdapter { 
     ArrayList<ConnectionsModel> data; 

     // constructor saves received data to `data` 
     // public view `getView()` displays it 
    } 
} 

我想要做明確的數據,並通知我在片段級做了onClick()後(或者,如果是,更容易明確ListView,直到我讀新數據從API - 所以我想清楚ListView當我從API讀取)...

回答

3

剛創建的適配器的公共方法,使私有變量,以保護其免受不必要的編輯或清除:

private ArrayList<ConnectionsModel> mData; 

public onClick() { 
    // do clear data and notify it (from ListView), how?! 
    clearData(); 
} 

public void clearData() { 
    mData.clear(); 
    // do something else here if you want. Like some kind of visual notification to the user 
    notifyDataSetChanged(); 
} 

如果你的onclick方法是在商建立適配器片段(稱之爲在這個例子中mAdapter),那麼的onclick是:

public onClick() { 
    // do clear data and notify it (from ListView), how?! 
    mAdapter.clearData(); 
} 
0

如果您getView()方法是從data ArrayList中拉,你應該可以撥打data.clear();然後notifyDataSetChanged();從您的ArrayAdapter

相關問題