Answer the question
In order to leave comments, you need to log in
How to update TextView on change in RecyclerView?
I have a CartActivity that displays a RecyclerView. There is also a RecyclerViewAdapter class, where a ClickListener is registered for ViewHolder, where an AlertDialog is displayed to the user with a suggestion to remove the selected item from the list.
CartActivity.java
...
while (cursor.moveToNext()) {
currentItem = new Item();
currentItem.setId( cursor.getInt(idColumnIndex));
currentItem.setName(cursor.getString(nameColumnIndex));
currentItem.setPrice(Double.parseDouble(cursor.getString(priceColumnIndex)));
currentItem.setAmount(Integer.parseInt(cursor.getString(amountColumnIndex)));
currentItem.setImage(cursor.getString(imageColumnIndex));
sumOrder += currentItem.getPrice()*currentItem.getAmount();
items.add(currentItem);
}
mAdapter = new RecyclerViewAdapter(items, R.layout.row, this);
mRecyclerView.setAdapter(mAdapter);
progressBar.setVisibility(View.GONE);
mSwipeRefreshLayout.setRefreshing(false);
txtSumOrderView.setText(getString(R.string.txt_sum_order, sumOrder));
...
RecyclerViewAdapter.java
...
viewHolder.itemView.setOnClickListener(view -> {
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
(dialog, which) -> {
items.remove(currentItem);
notifyDataSetChanged();
}
});
alertDialog.show();
}
...
Answer the question
In order to leave comments, you need to log in
because your data is stored in the database - there are several ways to do this:
1) use room - it will manage the data in the database itself. there are already quite a lot of examples on the Internet of working with room.
2) make the interface in the activity, declare the method and pass it to the adapter. In your case, it is better to process the processing of clicking on an element immediately in the activity - to do this, assign android:onClick to the desired element, then describe the method in the activity. For dynamic data updates in the adapter, there are also 2 ways:
a) each time to transfer a new cursor to the adapter - which can be overhead and lead to brakes during operation.
b) make a separate array, for example SparseIntArray, which will be responsible for the dynamic part. Pass it to the adapter, declare methods in the adapter to change dynamic data, and call mAdapter.notifyDataSetChanged() when the data changes.
In order to inform the adapter that the data has changed, there is a method notifyDataSetChanged();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question