There is a CartActivity activity in which RecyclerView is located. There is also a class RecyclerViewAdapter. For ViewHolder, ClickListener is registered, where the user is shown an AlertDialog with a suggestion to remove the selected item from the list.

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

How do you tell the activity that the items list array with RecyclerView has changed to recalculate the total amount of the remaining positions and update the text in the TextView element of the activity?

    1 answer 1

    Your architecture uses an unproductive solution with copying data from the database to a dynamic list and in addition to deleting from the list, you also need to do a separate deletion from the database (the first part of the answer, the solution for SimpleCursorAdapter ListView widget, but I think the problem is clear), which also adds confusion in implementation.

    I suggest you pay attention to the latest solutions in the organization of the architecture of the interaction of widgets with data proposed by Google - Architecture Components (the second part of the answer from the link above). With the advent of the adapter class for the 27.1.0 support library for the RecyclerView ListAdapter architecture has received a full interaction cycle and now any operations with data in the database are automatically displayed on the widget, that is, now you just have to write, delete, change the database and they will be right there displayed on the screen. Naturally, you will not need any duplication of data in lists, double data modification operations and other unnecessary problems.

    You can learn more about the new architecture for this series of lessons.

    It is also worth paying attention to the fact that it is better to entrust the calculations with the data in the database to the database itself, and that the adapter should only be directly involved in outputting to the screen, rather than calculations. In this case, you calculate the sumOrder value in the adapter; you can trust this database using calculated columns. For example:

     SELECT price, count, price*count AS sum FROM items 

    Here in the sample we get 3 columns: the values ​​from the price , count columns and the calculated column sum (it will be added to the sample, although there is no such column in the table), with the product of the first two columns. Now you can simply take data from there with the desired result, rather than counting it in the adapter.

    • realm (a) does have a component (RealmAdapter), and then added it to the support library? - Timur Mukhortov
    • @TimurMukhortov No, these are completely different adapters. The first works with the Realm database and its RealmResult data, the second with the components of the Architecture Components and their LiveData datapavlofff
    • I understand that the adapters are different. Thank. Application and the result will be the same? - Timur Mukhortov
    • one
      @TimurMukhortov There are some subtleties of use there, in particular the mandatory implementation of the DiffUtil DiffUtil and receiving data in the adapter not through the constructor, but through the getItem() method getItem() you don’t need to send anything to the adapter), but in general, yes. You work with the database - the list itself displays all changes. Here is an article with explanations - pavlofff