[英]How to refresh two listview at one moment in Android?
我有兩個listview,例如listview_1
和listview_2
。 我想刷新listview_2
而listview_1
被刷新。
我的代碼是這樣的:
public void updateTwoListView() {
listview_1.getAdapter().notifyDataSetChanged();
listview_2.getAdapter().notifyDataSetChanged();
}
但它不起作用, listview_1
可以刷新,但listview_2
不能。
在那一刻,我發現listview_1
是焦點。
然后我嘗試在運行方法之前將焦點設置為其他視圖,兩者都沒有刷新。 它僅在listview具有焦點時才刷新列表視圖。
我發現當我調用方法刷新時, listview_2
沒有,然后我將焦點設置為listview_2
,刷新了自己!
那么,我想問的是:
如何在Android中的某個時刻刷新兩個listview?
還有更多代碼:
//init two listview there
public void init() {
listview_1 = (ListView)findViewById(R.id.listView1);
listview_2 = (ListView)findViewById(R.id.listView2);
adapter1 = new MyListViewAdapter(mContext);
adapter2 = new MyListViewAdapter(mContext); //I have tried use different adapter, that also didn't work.
listview_1.setAdapter(adapter1);
listView_2.setAdapter(adapter2);
}
上行代碼段的真正代碼是:
public void updateTwoListView(int currentPosition) {
adapter1.updateCurPos(currentPosition);
adapter2.updateCurPos(currentPosition);
}
在MyListViewAdapter.java中:
public void updateCurPos(int currentPosition) {
mCurrentPos = currentPosition;
notifyDataSetChanged();
}
我將調用listViewManager.updateTwoListView(1)
類的方法來刷新。
任何回復表示贊賞。
您已兩次調用listview_1
。 只需將其中一個更改為listview_2
,如下所示:
public void updateTwoListView() {
listview_1.getAdapter().notifyDataSetChanged();
listview_2.getAdapter().notifyDataSetChanged();
}
您的代碼似乎是調用getAdapter()
。
設置此ListView背后的數據。 傳遞給此方法的適配器可能由WrapperListAdapter包裝,具體取決於當前使用的ListView功能。 例如,添加頁眉和/或頁腳將導致適配器被包裝。
資料來源: http : //developer.android.com/reference/android/widget/ListView.html#setAdapter(android.widget.ListAdapter)
解決方案是將您的Adapter
保存為活動中的成員變量,並從那里調用notifyDataSetChanged()
。
有關此問題的答案,請參閱: https : //stackoverflow.com/a/31893525/2742759
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.