Reputation: 2720
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
Upvotes: 0
Views: 1203
Reputation: 3863
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(); }
Upvotes: 2