本文介绍了Android Update Recycler查看项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过新数据更新recyclerview项目时遇到一个简单的问题,我想知道哪个代码可以在不使用数据绑定的情况下正常工作,在我的代码中通过单击按钮作为clickOnSendCommandToRobot方法添加新项目触发,此方法必须向List添加新项目,我的适配器可以知道要添加新数据并刷新RecyclerView,我没有收到任何错误,但列表大小始终为1

i have simple problem on update recyclerview items by new data, i'm wondering which code work fine without using data binding, in my code adding new items trigger by click on button as clickOnSendCommandToRobot method, this method must add new item into List and my adapter can be know that to add new data and refresh RecyclerView, i dont get any error, but list size is 1 always

public class ActivityRegister extends BaseActivities {
    private RobotMessagesAdapter adapter;
    private List<RobotViewModel> model;
    private static final String TAG = "register";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_register);

        ActivityRegisterPresenter presenter = new ActivityRegisterPresenter(this);
        ActivityRegisterViewModel viewModel = new ActivityRegisterViewModel();
        binding.setViewModel(viewModel);
        binding.setPresenter(presenter);

        if (savedInstanceState == null) {
            model = new ArrayList<>();
        } else {
            model = savedInstanceState.getParcelableArrayList("model");
        }
        adapter = new RobotMessagesAdapter(this, model);
        binding.registerRobot.setAdapter(adapter);

        ...
    }
    @Override
    public void clickOnSendCommandToRobot() {
        RobotViewModel temp = new RobotViewModel();
        temp.setMessage(Math.round(Math.random()) + "");
        temp.setMessageType(SV.RobotMessageType.SENT_BY_ROBOT.ordinal());
        model.add(temp);

        Log.e(TAG, model.size() + "");

        adapter.setData(model);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putParcelableArrayList("model", (ArrayList) model);
        super.onSaveInstanceState(outState);
    }
}

适配器setData方法:

public void setData(List<RobotViewModel> data) {
    Log.e("data size ", data.size() + "");
    list.clear();
    list.addAll(data);
    notifyDataSetChanged();
}

每次单击按钮

并触发clickOnSendCommandToRobot方法. model变量清除,并且我没有最新添加的项,并且适配器不按新添加的项刷新列表.Logcat:

for each clicking on button and trigger clickOnSendCommandToRobot method. model variable clear and i don't have latest added items into that and adapter dont refresh list by new added item.Logcat:

04-08 08:29:45.342 21470-21470/com.sample.myapp E/register: 1
04-08 08:29:45.343 21470-21470/com.sample.myapp E/data size: 1
04-08 08:29:46.658 21470-21470/com.sample.myapp E/register: 1
04-08 08:29:46.658 21470-21470/com.sample.myapp E/data size: 1
04-08 08:29:47.462 21470-21470/com.sample.myapp E/register: 1
04-08 08:29:47.462 21470-21470/com.sample.myapp E/data size: 1

推荐答案

由于模型是对回收视图适配器本身的引用,因此对外部模型对象所做的任何更改都将链接回适配器.刚好位于,请在其中调用notifyDataSetChange.因此adapter.setData(model);删除并只需执行adapter.notifyDatasetChange();

Because the model is a reference to the recycler view adapter itself, any changes made to the outside model object links back to adapter.So outside your adapter class where model is located just, call notifyDataSetChange there.so adapter.setData(model); remove and simply do adapter.notifyDatasetChange();

 @Override
    public void clickOnSendCommandToRobot() {
        RobotViewModel temp = new RobotViewModel();
        temp.setMessage(Math.round(Math.random()) + "");
        temp.setMessageType(SV.RobotMessageType.SENT_BY_ROBOT.ordinal());
        model.add(temp);

        Log.e(TAG, model.size() + "");

        adapter.notifyDataSetChanged();        }

这篇关于Android Update Recycler查看项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 09:33