简体   繁体   English

如何在ui线程上刷新回收器视图

[英]How to refresh recycler view on ui thread

I have a recycler view in my app and I am getting values from the user and storing it in the SQLite database and then those values will display in recycler view by retrieving.我的应用程序中有一个回收器视图,我从用户那里获取值并将其存储在 SQLite 数据库中,然后这些值将通过检索显示在回收器视图中。 problem is that the recycler view is only updating when I reopen my activity so how can I refresh the recycler view without reopening the activity should I use UI thread?问题是回收器视图仅在我重新打开我的活动时更新,所以我如何在不重新打开活动的情况下刷新回收器视图,我应该使用 UI 线程吗? images: after inserting item图像:插入项目后

after reopen acitivty I want to display inserted item on recyclearview when the user click on the insert button重新打开活动后,当用户单击插入按钮时,我想在 recyclearview 上显示插入的项目

here is my code:这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText=findViewById(R.id.ed1);
        button=findViewById(R.id.INSERT);
        name=new ArrayList<>() ;
        id=new ArrayList<>();
        recyclerView=findViewById(R.id.res);
        database=new Database(MainActivity.this);

                button.setOnClickListener(v -> {
                    database.insert(editText.getText().toString().trim());//insert
                       adapter.notifyDataSetChanged();
   
                });

StoreDataInArray();//for store database data in array
adapter=new Adapter(name,id,MainActivity.this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

        }

You can use method你可以使用方法

runOnUiThread(new Runnable() {
    public void run() {
        adapter.notifyDataSetChanged();
    }
});

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

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