0
我在片段中有一個ListView,我想在其中添加新項目。 DialogFragment用於輸入新項目,按「是」時,項目動態添加到ListView。我嘗試了很多解決方案,但是我無法實現它的功能。在對話框片段中更新片段內的列表視圖
我在片段中有一個ListView,我想在其中添加新項目。 DialogFragment用於輸入新項目,按「是」時,項目動態添加到ListView。我嘗試了很多解決方案,但是我無法實現它的功能。在對話框片段中更新片段內的列表視圖
我會回答我的問題。
您的活動實現了接口在DialogFragment定義
public void onCreateMatchDialogPositiveClick(DialogFragment dialog , String itemsToAdd) {
//Your Fragment Class containing listview to be updated
MyListViewFragmentClass fragment = (MyListViewFragmentClass) getSupportFragmentManager().findFragmentById(R.id.myListview);
if(fragment != null){
//fragment is available
fragment.addItemToListView(itemsToAdd);
}
}
然後在含有列表視圖中添加動態項目的片段
public void addItemToListView(itemToAdd){
matchList.add(itemToAdd); //Add item to your dataset
adapter.notifyDataSetChanged();//Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh
}
您可以發佈兩個類的完整代碼?我被困在相同的情況 –
這裏是關於同樣情況的好文章:https://wuyingren.github.io/howto/2013/08/23/Updating-a-ListView-inside-a-Fragment-placed-on-一個-ViewPager-ON-DialogFragment-解僱/ – Amir