The result is a MainActivity that implements the click interface from the adapter. I try to implement adding to favorites, but when I click on the "To favorites" button, nothing happens on the list item.

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>, BludaAdapter.OnItemClickListener { private BludaAdapter bludaAdapter; private int currentLoader; final private static int LOADER_BLUDA = 0; RecyclerView rvList; DBHelper db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.rvList); LinearLayoutManager layoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(layoutManager); DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, layoutManager.getOrientation()); recyclerView.addItemDecoration(dividerItemDecoration); db = new DBHelper(this); rvList = (RecyclerView) findViewById(R.id.rvList); bludaAdapter = new BludaAdapter(this); rvList.setAdapter(bludaAdapter); bludaAdapter.setOnItemClickListener(this); Bundle bundle = new Bundle(1); bundle.putString("filter", ""); getSupportLoaderManager().initLoader(LOADER_BLUDA, bundle, this); } @Override public void onItemClick(long id) { // пСрСмСнная id - ID записи, ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ ΠΊΠ»ΠΈΠΊΠ½ΡƒΠ»ΠΈ Π² спискС Toast.makeText(this, id+"", Toast.LENGTH_SHORT).show(); } @Override public void onFavoriteButtonClick(long id, boolean isFavorite) { // пСрСмСнная id - ID записи, ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ ΠΊΠ»ΠΈΠΊΠ½ΡƒΠ»ΠΈ (для обращСния ΠΊ Π‘Π”) // пСрСмСнная isFavorite - состояниС ΠΈΠ·Π±Ρ€Π°Π½Π½ΠΎΠ³ΠΎ Π² записи ΠΏΠ΅Ρ€Π΅Π΄ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ΠΌ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ Toast.makeText(this, isFavorite+"", Toast.LENGTH_SHORT).show(); int fav = 0; String table = Contract.Bluda.TAB_BLUDA; if (isFavorite) { fav = 0; isFavorite = false; Toast.makeText(this, "fav = 0", Toast.LENGTH_SHORT).show(); } else { fav = 1; isFavorite = true; Toast.makeText(this, "fav = 1", Toast.LENGTH_SHORT).show(); } ContentValues values = new ContentValues(); values.put(Contract.Bluda.COL_LIKE, fav); long newRowId = db.database.update(table, values, Contract.Bluda._ID + "= " + id, null); if (newRowId == -1) { Toast.makeText(this, "Ошибка", Toast.LENGTH_SHORT).show(); } else { getSupportLoaderManager().getLoader(currentLoader).forceLoad(); } } @Override protected void onResume() { super.onResume(); } @Override public Loader<Cursor> onCreateLoader(int id, Bundle bundle) { return new MyCursorLoader(this, db, id); } @Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { bludaAdapter.swapCursor(data); } @Override public void onLoaderReset(Loader<Cursor> loader) { bludaAdapter.swapCursor(null); } static class MyCursorLoader extends CursorLoader { Cursor cursor; DBHelper dbHeler; final int loaderID; public MyCursorLoader(Context context, DBHelper dbHeler, int id) { super(context); this.dbHeler = dbHeler; loaderID = id; } @Override protected Cursor onLoadInBackground() { switch (loaderID) { case LOADER_BLUDA: cursor = dbHeler.getBluda(); break; } return cursor; } } } 
  • what does "nothing happens"? does the onFavoriteButtonClick() method not work? Record in a DB is not updated? There are no changes in the list after clicking? - pavlofff
  • The method itself works, but in theory, when you click on a button, it should be added to favorites (ie, the like field should change) and the recording on the button itself should change, but this will not happen - Natalya Sergeevna
  • that is, when you restart the item that you have added to your favorites not in your favorites? - pavlofff
  • It seems to be yes .. In the adapter, you indicated for the method of clicking on the favorites like == 1, please explain. In the activation will always be transmitted the value 1? Or is this a check, and if the like field equals 1, then will true be returned otherwise false? - Natalya Sergeevna
  • Already in favorites. I changed the code a bit in the click method of the favorites button. Updated the question. Can you take a look and tell if you can leave it this way or better? - Natalya Sergeevna

0