There is a list of news, when you go to the news, the information in the new activation opens. I try to make it so that after the user read the news the title color changed. It was decided to replace the SharedPreference implementation SharedPreference a SQLite database.

First of all, I created the class TopStoryDatabaseHelper , where I save the Id :

 public class TopStoryDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "topstory"; private static final int DB_VERSION = 1; public TopStoryDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("CREATE TABLE TOPSTORY(" + " _id INTEGER PRIMARY KEY AUTOINCREMENT;) "); insertId(sqLiteDatabase); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) { } private static void insertId(SQLiteDatabase sqLiteDatabase) { ContentValues idValues = new ContentValues(); sqLiteDatabase.insert("TOPSTORY", null, idValues); } } 

The logic is the same, the user clicks on the news - the news opens - the id is saved - after which the news is considered to be read - we change the color for the read news. So as the user adds the nth number of id, I would like to clarify 1. Do I need to add all the fields for my news to the database? 2. After that, how to select from the activit class by id correctly.

  • Little is clear from the code snippet you cited. - post_zeew
  • @post_zeew I have an id which I save in a sharedprefence and then for each clicked news I check the presence of id. And if it is, then I fulfill the condition. - Morozov
  • My first comment was that you show little code. - post_zeew
  • 2
    A couple of days ago you were advised to use your database for solving your problem, for some reason you ignore this correct method. Although it is possible to solve your problem with the help of SharedPreferences , this solution will be wrong from the point of view of architecture , and if something changes, you will need to change everything drastically . And yes, save the state of each news with different keys. Will work ... for now. But this is not a good way to solve this kind of problems. - post_zeew
  • 2
    The fact that you have not yet matured for a database does not mean at all that you need to solve problems with crutches , but it means that it’s time to learn how to work with databases. And the answer to your question is in my previous comment. - post_zeew

1 answer 1

1) Every time you click, you go to the body of the news and there you save the ID in the database that you have already done

2) Every time when you upload news in the RecyclerView , in the adapter in the onBindViewHolder() method you will check the ID Новости that you just downloaded from the server with the ID that is in the database. And if they are equal, then change the color of the title.

Here is a piece of code for viewed:

From the database I get the ID all the vacancies and stored in viewedVacanciesIDList.

 SQLHelper sqlHelper = new SQLHelper(mContext); HashSet<Integer> viewedVacanciesIDList = sqlHelper.haveWatchedVacancies(); 

where haveWatchedVacancies() returns from the database the ID all vacancies.

 public HashSet<Integer> haveWatchedVacancies() { SQLiteDatabase db = this.getWritableDatabase(); HashSet<Integer> watchedVacanciesList = new HashSet<>(); Cursor cursor = db.query(true, WATCHED.TABLE_NAME, null, null, null, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { if (cursor.moveToFirst()) { int vacancyIdIndex = cursor.getColumnIndex(WATCHED.KEY_VACANCY_ID); do { int getVacancyID = cursor.getInt(vacancyIdIndex); watchedVacanciesList.add(getVacancyID); } while (cursor.moveToNext()); } else { cursor.close(); db.close(); } } else { Log.d("WATCHED TABLE", "empty db"); } return watchedVacanciesList; } 

And viewedVacanciesIDList for storing the obtained data from the database locally, well, so that every time you check, you do not have to access the database, such as

 if (sqlHelpter.haveWatchedVacancies().contains(model.getId()) 

And I do a check.

 holder.vacancyViewed.setVisibility(View.GONE); if (viewedVacanciesIDList.contains(model.getId())) { holder.vacancyViewed.setVisibility(View.VISIBLE); } 

And you do not need to make invisible, even easier

 if (viewedVacanciesIDList.contains(model.getId())) { holder.vacancyViewed.setBackgroundColor(Color.RED); } 
  • Could you please clarify haveWatchedVacancies and what is viewedVac forVidanVIDIDList and how should I register it in a DB class? (+ I have a recyclerView). - Morozov
  • updated answer, please check - DevOma