В моем приложении есть возможность добавить / удалить избранное. Но это становится фаворитом с arraylist. Даже если я удалю элементы из списка, когда закрою и снова открою приложение, элемент вернется. Каждая категория имеет свою таблицу в моей структуре базы данных. Итак, если пользователь в категории, он может добавить или удалить избранное. Но на странице избранного я не могу удалить избранное. Можете ли вы, ребята, взглянуть на коды и подсказать мне, как решить, пожалуйста? получить любимые предметы
case 20:
favList.addAll(db.getFavFacts(Fact.TABLE_GENERAL_NAME, Fact.GENERAL_FACT_ID, Fact.GENERAL_FACT_NAME, Fact.GENERAL_FACT_FAV));
favList.addAll(db.getFavFacts(Fact.TABLE_ANIMAL_NAME, Fact.ANIMAL_FACT_ID, Fact.ANIMAL_FACT_NAME,Fact.ANIMAL_FACT_FAV));
...
if(favList.isEmpty()){
factTextView.setText("You have no favorite facts yet!");
textViewId.setText(null);
} else if(favList.size() == 1){
factTextView.setText(favList.get(readFromShared("FavoriteKey")-2).getFact());
textViewId.setText("Fact "+ (readFromShared("FavoriteKey")-1)+ " of " + favList.size());
}
else {
factTextView.setText(favList.get(readFromShared("FavoriteKey")-1).getFact());
textViewId.setText("Fact "+ (readFromShared("FavoriteKey"))+ " of " + favList.size());
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
onSwipeFavMethod(event,favList,"FavoriteKey");
return true;
}});
break;
}
}
мой факт. java
package com.example.didyouknow;
public class Fact {
//GENERAL
public static String TABLE_GENERAL_NAME = "general_fact_table";
public static String GENERAL_FACT_NAME = "general_fact_name";
public static String GENERAL_FACT_ID = "general_fact_id";
public static String GENERAL_FACT_FAV = "general_fact_fav";
public static final String CREATE_GENERAL_TABLE =
"CREATE TABLE " + TABLE_GENERAL_NAME + "("
+ GENERAL_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ GENERAL_FACT_NAME + " TEXT,"
+ GENERAL_FACT_FAV + " BOOLEAN"
+ ")";
//ANIMAL
public static String TABLE_ANIMAL_NAME = "animal_fact_table";
public static String ANIMAL_FACT_NAME = "animal_fact_name";
public static String ANIMAL_FACT_ID = "animal_fact_id";
public static String ANIMAL_FACT_FAV = "animal_fact_fav";
public static final String CREATE_ANIMAL_TABLE =
"CREATE TABLE " + TABLE_ANIMAL_NAME + "("
+ ANIMAL_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ANIMAL_FACT_NAME + " TEXT,"
+ ANIMAL_FACT_FAV + " BOOLEAN"
+ ")";
//COMPUTER
public static String TABLE_COMPUTER_NAME = "computer_fact_table";
public static String COMPUTER_FACT_NAME = "computer_fact_name";
public static String COMPUTER_FACT_ID = "computer_fact_id";
public static String COMPUTER_FACT_FAV = "computer_fact_fav";
public static final String CREATE_COMPUTER_TABLE =
"CREATE TABLE " + TABLE_COMPUTER_NAME + "("
+ COMPUTER_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COMPUTER_FACT_NAME + " TEXT,"
+ COMPUTER_FACT_FAV + " BOOLEAN"
+ ")";
//COUNTRIES
public static String TABLE_COUNTRIES_NAME = "countries_fact_table";
public static String COUNTRIES_FACT_NAME = "countries_fact_name";
public static String COUNTRIES_FACT_ID = "countries_fact_id";
public static String COUNTRIES_FACT_FAV = "countries_fact_fav";
public static final String CREATE_COUNTRIES_TABLE =
"CREATE TABLE " + TABLE_COUNTRIES_NAME + "("
+ COUNTRIES_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COUNTRIES_FACT_NAME + " TEXT,"
+ COUNTRIES_FACT_FAV + " BOOLEAN"
+ ")";
//FOOD
public static String TABLE_FOOD_NAME = "food_fact_table";
public static String FOOD_FACT_NAME = "food_fact_name";
public static String FOOD_FACT_ID = "food_fact_id";
public static String FOOD_FACT_FAV = "food_fact_fav";
public static final String CREATE_FOOD_TABLE =
"CREATE TABLE " + TABLE_FOOD_NAME + "("
+ FOOD_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ FOOD_FACT_NAME + " TEXT,"
+ FOOD_FACT_FAV + " BOOLEAN"
+ ")";
//HUMANBODY
public static String TABLE_HUMANBODY_NAME = "humanbody_fact_table";
public static String HUMANBODY_FACT_NAME = "humanbody_fact_name";
public static String HUMANBODY_FACT_ID = "humanbody_fact_id";
public static String HUMANBODY_FACT_FAV = "humanbody_fact_fav";
public static final String CREATE_HUMANBODY_TABLE =
"CREATE TABLE " + TABLE_HUMANBODY_NAME + "("
+ HUMANBODY_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ HUMANBODY_FACT_NAME + " TEXT,"
+ HUMANBODY_FACT_FAV + " BOOLEAN"
+ ")";
//PEOPLE
public static String TABLE_PEOPLE_NAME = "people_fact_table";
public static String PEOPLE_FACT_NAME = "people_fact_name";
public static String PEOPLE_FACT_ID = "people_fact_id";
public static String PEOPLE_FACT_FAV = "people_fact_fav";
public static final String CREATE_PEOPLE_TABLE =
"CREATE TABLE " + TABLE_PEOPLE_NAME + "("
+ PEOPLE_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ PEOPLE_FACT_NAME + " TEXT,"
+ PEOPLE_FACT_FAV + " BOOLEAN"
+ ")";
//PSYCHOLOGY
public static String TABLE_PSYCHOLOGY_NAME = "psychology_fact_table";
public static String PSYCHOLOGY_FACT_NAME = "psychology_fact_name";
public static String PSYCHOLOGY_FACT_ID = "psychology_fact_id";
public static String PSYCHOLOGY_FACT_FAV = "psychology_fact_fav";
public static final String CREATE_PSYCHOLOGY_TABLE =
"CREATE TABLE " + TABLE_PSYCHOLOGY_NAME + "("
+ PSYCHOLOGY_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ PSYCHOLOGY_FACT_NAME + " TEXT,"
+ PSYCHOLOGY_FACT_FAV + " BOOLEAN"
+ ")";
//SCIENCE
public static String TABLE_SCIENCE_NAME = "science_fact_table";
public static String SCIENCE_FACT_NAME = "science_fact_name";
public static String SCIENCE_FACT_ID = "science_fact_id";
public static String SCIENCE_FACT_FAV = "science_fact_fav";
public static final String CREATE_SCIENCE_TABLE =
"CREATE TABLE " + TABLE_SCIENCE_NAME + "("
+ SCIENCE_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ SCIENCE_FACT_NAME + " TEXT,"
+ SCIENCE_FACT_FAV + " BOOLEAN"
+ ")";
//UNIVERSE
public static String TABLE_UNIVERSE_NAME = "universe_fact_table";
public static String UNIVERSE_FACT_NAME = "universe_fact_name";
public static String UNIVERSE_FACT_ID = "universe_fact_id";
public static String UNIVERSE_FACT_FAV = "universe_fact_fav";
public static final String CREATE_UNIVERSE_TABLE =
"CREATE TABLE " + TABLE_UNIVERSE_NAME + "("
+ UNIVERSE_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ UNIVERSE_FACT_NAME + " TEXT,"
+ UNIVERSE_FACT_FAV + " BOOLEAN"
+ ")";
//WEATHER
public static String TABLE_WEATHER_NAME = "weather_fact_table";
public static String WEATHER_FACT_NAME = "weather_fact_name";
public static String WEATHER_FACT_ID = "weather_fact_id";
public static String WEATHER_FACT_FAV = "weather_fact_fav";
public static final String CREATE_WEATHER_TABLE =
"CREATE TABLE " + TABLE_WEATHER_NAME + "("
+ WEATHER_FACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ WEATHER_FACT_NAME + " TEXT,"
+ WEATHER_FACT_FAV + " BOOLEAN"
+ ")";
private String fact;
private int factId;
private int favorite;
public Fact(){
}
public Fact(int factId, String fact, int favorite) {
this.fact = fact;
this.factId = factId;
this.favorite = favorite;
}
public String getFact() {
return fact;
}
public void setFact(String fact) {
this.fact = fact;
}
public int getFactId() {
return factId;
}
public void setFactId(int factId) {
this.factId = factId;
}
public int isFavorite() {
return favorite;
}
public void setFavorite(int favorite) {
this.favorite = favorite;
}
}
моя база. java
package com.example.didyouknow;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
public class DatabaseFacts extends SQLiteOpenHelper {
private static final String dbName = "facts_db";
private static final int dbVersion = 3;
private Context context;
public DatabaseFacts(Context context) {
super(context, dbName, null, dbVersion);
this.context = context;
this.copyDbIfNotExists();
}
public void copyDbIfNotExists() {
// Ensure /data/data/YOUR_PACKAGE_NAME/databases/ directory is created.
File dbDir = new File(context.getDatabasePath(dbName).getParentFile().getPath());
if (!dbDir.exists())
dbDir.mkdir();
// Copy database starts here.
String appDbPath = this.context.getDatabasePath(dbName).getAbsolutePath();
File dbFile = new File(appDbPath);
if (!dbFile.exists()) {
try {
InputStream mInput = context.getAssets().open("facts_db");
OutputStream mOutput = new FileOutputStream(appDbPath);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer, 0, 1024)) > 0)
mOutput.write(mBuffer, 0, mLength);
mOutput.flush();
mOutput.close();
mInput.close();
} catch (IOException ex) {
throw new Error("Error copying database: " + ex.getMessage());
}
}
}
@Override
public void onCreate(SQLiteDatabase db) {
// db.execSQL(Fact.CREATE_GENERAL_TABLE);
// db.execSQL(Fact.CREATE_ANIMAL_TABLE);
// db.execSQL(Fact.CREATE_COMPUTER_TABLE);
// db.execSQL(Fact.CREATE_COUNTRIES_TABLE);
.
.
.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// db.execSQL("DROP TABLE IF EXISTS " + Fact.TABLE_GENERAL_NAME);
// db.execSQL("DROP TABLE IF EXISTS " + Fact.TABLE_ANIMAL_NAME);
// db.execSQL("DROP TABLE IF EXISTS " + Fact.TABLE_COMPUTER_NAME);
....
// onCreate(db);
}
public long insertFact(String fact, Boolean fav, String factFav, String factName, String tableName) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(factFav, fav);
values.put(factName, fact);
long id = db.insert(tableName, null, values);
db.close();
return id;
}
public Fact getFact(long id, String tableName, String factId, String factName, String factFav) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(tableName,
new String[]{factId, factName, factFav},
factId + "=?",
new String[]{String.valueOf(id)}, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Fact fact = new Fact(
cursor.getInt(cursor.getColumnIndex(factId)),
cursor.getString(cursor.getColumnIndex(factName)),
cursor.getInt(cursor.getColumnIndex(factFav)));
cursor.close();
return fact;
}
public ArrayList<Fact> getFavFacts(String tableName, String factId, String factName, String factFav) {
ArrayList<Fact> facts = new ArrayList<>();
String selectQuery = "SELECT * FROM " + tableName + " WHERE " + factFav + "= 1";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Fact fact = new Fact();
fact.setFactId(cursor.getInt(cursor.getColumnIndex(factId)));
fact.setFact(cursor.getString(cursor.getColumnIndex(factName)));
fact.setFavorite(Integer.valueOf(cursor.getString(cursor.getColumnIndex(factFav))));
facts.add(fact);
} while (cursor.moveToNext());
}
db.close();
return facts;
}
public int getFactsCount(String tableName) {
String countQuery = "SELECT * FROM " + tableName;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
return count;
}
public int updateFact(int factId, String factColumnId, Boolean fav, String factFav, String tableName, String factName, String name) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(factFav, fav);
values.put(factName, name);
// updating row
return db.update(tableName, values, factColumnId + " = ?",
new String[]{String.valueOf(factId)});
}
// public boolean isFavorite(int factId, String factColumnId, Boolean fav, String factFav, String tableName, String factName, String name){
// SQLiteDatabase db = this.getReadableDatabase();
// Cursor cursor = db.query(tableName, )
// }
}