2016-04-15 60 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

相关问题