Android update recyclerView items

╄→尐↘猪︶ㄣ 提交于 2019-12-25 12:50:49

问题


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);
    }
}

adapter setData method:

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

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

回答1:


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();        }


来源:https://stackoverflow.com/questions/43269050/android-update-recyclerview-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!