简体   繁体   English

NotifyDataSetChanged在不同的片段上

[英]NotifyDataSetChanged on different fragment

I'm using a Master / Detail flow (not the default one), so I have one fragment that contains the stuff of the RecyclerView and when I click on a item I see on other fragment the detail of that item, it works, but now I want to delete this item from the detail fragment, and when I click on delete it refreshes and then notifydatasetchanged. 我使用的是“主/明细”流程(不是默认流程),所以我有一个包含RecyclerView内容的fragment ,当我单击某个项目时,在另一个fragment上看到该项目的细节,它可以工作,但是现在我想从详细信息片段中删除该项目,当我单击“删除”时,它会刷新,然后notifydatasetchanged。

What's the problem? 有什么问题? I have the Adapter on my ListFragment and on my DetailFragment I only can delete the item from my SQLite nothing else, I'd like to refresh the list. 我在我的ListFragment和我的DetailFragment上都有适配器,我只能从SQLite删除该项目,我想刷新列表。

Activity -- Where I create the listFragment 活动-我在哪里创建listFragment

Fragment1 -- ListFragment (RecyclerView, Adapter, Sqlite(For fetch products)) Fragment1-ListFragment(RecyclerView,Adapter,Sqlite(用于提取产品))

Fragment2 -- DetailFragment (Sqlite(to delete item)) Fragment2-DetailFragment(Sqlite(删除项目))

Activity 活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    if(savedInstanceState == null){
        Fragment list = new ListFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.list,list).commit();
    }
}

On my ListFragment I have those attributes 在我的ListFragment我有那些属性

private SQLiteAdapter sqliteAdapter;
private Context mContext;
private Adapter adapter;
private RecyclerView recyclerView;
private List<Product> data;

Then I put the data with a method that open the sqliteAdapter and then fetch all products and then closses the connection. 然后,我使用打开sqliteAdapter的方法放入数据,然后获取所有产品,然后关闭连接。

On my DetailFragment I have the SQLite object to get the product sent from the adapter (id product) and then show info about this product. 在我的DetailFragment上,我有SQLite对象来获取从适配器发送的产品(id产品),然后显示有关该产品的信息。 Now I can delete an object, but it's not refreshing from ListFragment, I'm wondering where to put this SQLite object on Activity or in both fragments is ok 现在我可以删除一个对象,但是它不能从ListFragment刷新,我想知道将此SQLite对象放在Activity上还是两个片段中都可以

It sounds like you are only deleting the item from your database. 听起来您只是从数据库中删除项目。 You also need to delete it from the adapter's list or other data structure. 您还需要将其从适配器的列表或其他数据结构中删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM