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; } } }
onFavoriteButtonClick()method not work? Record in a DB is not updated? There are no changes in the list after clicking? - pavlofff