(MainActivity)
(Dashboard Activity)
(FavActivity)
STEP 1: XML of MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
tools:context=".Activity.MainActivity">
<Button
android:id="@+id/btn_dashboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="DASH BOARD" />
<Button
android:id="@+id/btn_fav_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="FAVOURITE LIST" />
</LinearLayout>
STEP 2: XML of DashboardActivity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.DashBordActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
STEP 3: XML of FavListActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.FavListActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
STEP 4: XML of Item Dashboard
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_fruit"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/app_name"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_notFav"
app:layout_constraintStart_toEndOf="@+id/iv_fruit"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_notFav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/fav_line"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_Fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/fav_fill"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#696969"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
STEP 5: XML of Item Fav
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_fruit"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/app_name"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_fav"
app:layout_constraintStart_toEndOf="@+id/iv_fruit"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/fav_fill"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#696969"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
---------------------------------------------------------
STEP 6: Create Database
public class MyDatabase extends SQLiteOpenHelper {
public MyDatabase(@Nullable Context context) {
super(context, "mydb.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "create table bookmark(pos integer primary key, name text, image int)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
String query = "drop table if exists bookmark";
db.execSQL(query);
}
public void insertData(String pos, String name, int image) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("pos", pos);
contentValues.put("name", name);
contentValues.put("image", image);
database.insert("bookmark", null, contentValues);
}
public void removeData(String pos) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("pos", pos);
long res = database.delete("bookmark", "pos=?", new String[]{pos});
if (res == 1) {
Log.e("student", "removeData: success" + res);
} else {
Log.e("student", "removeData: failed!..........................." + res);
}
}
public Cursor getData() {
SQLiteDatabase database = getReadableDatabase();
String query = "select * from bookmark";
Cursor cursor = database.rawQuery(query, null);
return cursor;
}
}
STEP 7: Create Model Class
public class ModelFavList {
String pos;
String name;
int image;
public ModelFavList(String pos, String name, int image) {
this.pos = pos;
this.name = name;
this.image = image;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getPos() {
return pos;
}
public void setPos(String pos) {
this.pos = pos;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
STEP 8: JAVA of MainActivity
public class MainActivity extends AppCompatActivity {
private Button btn_dashboard, btn_fav_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_dashboard = findViewById(R.id.btn_dashboard);
btn_fav_list = findViewById(R.id.btn_fav_list);
btn_dashboard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DashBordActivity.class));
}
});
btn_fav_list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, FavListActivity.class));
}
});
}
}
STEP 9: JAVA of DashBoardActivity
public class DashBordActivity extends AppCompatActivity {
private DashbordAdapter dashbordAdapter;
private RecyclerView rv;
private String[] name = {"Apple", "Banana", "Cherry", "Grapes", "Kiwi", "Mango", "Orange", "Papaya", "Pineapple", "Strawberry", "Watermelone"};
private int[] image = {
R.drawable.fruit_apple,
R.drawable.fruit_banana,
R.drawable.fruit_cherry,
R.drawable.fruit_grapes,
R.drawable.fruit_kiwi,
R.drawable.fruit_mango,
R.drawable.fruit_orange,
R.drawable.fruit_papaya,
R.drawable.fruit_pineapple,
R.drawable.fruit_strawberry,
R.drawable.fruit_watermelon,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_bord);
rv = findViewById(R.id.rv);
dashbordAdapter = new DashbordAdapter(this, name, image);
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
rv.setLayoutManager(manager);
rv.setAdapter(dashbordAdapter);
}
}
STEP 10: JAVA of FavListActivity
public class FavListActivity extends AppCompatActivity {
MyDatabase myDatabase;
RecyclerView recyclerView;
List<ModelFavList> dataList = new ArrayList<>();
FavAdapter favAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fav_list);
recyclerView = findViewById(R.id.recyclerView);
myDatabase = new MyDatabase(this);
favAdapter = new FavAdapter(this, dataList);
refreshData();
}
public void refreshData() {
favAdapter.clearRecyclerView();
Cursor cursor = myDatabase.getData();
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String pos = cursor.getString(0);
String name = cursor.getString(1);
int image = cursor.getInt(2);
ModelFavList modelFavList = new ModelFavList(pos, name, image);
dataList.add(modelFavList);
cursor.moveToNext();
}
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(favAdapter);
}
}
STEP 11: JAVA of DashBoard Adapter
public class DashbordAdapter extends RecyclerView.Adapter<DashbordAdapter.DataHolder> {
Context context;
String[] name;
int[] image;
LayoutInflater inflater;
//boolean isFav = false;
MyDatabase myDatabase;
public DashbordAdapter(DashBordActivity dashBordActivity, String[] name, int[] image) {
context = dashBordActivity;
this.name = name;
this.image=image;
inflater = LayoutInflater.from(context);
myDatabase = new MyDatabase(context);
}
@NonNull
@Override
public DataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_dashbord, parent, false);
return new DataHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DataHolder holder, int position) {
//TODO: open fav listed data...
Cursor cursor = myDatabase.getData();
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String pos = cursor.getString(0);
String name = cursor.getString(1);
String image = cursor.getString(2);
if (pos.equals(String.valueOf(position))) {
// holder.iv_fav.setImageResource(R.drawable.fav_fill);
holder.iv_Fav.setVisibility(View.VISIBLE);
holder.iv_notFav.setVisibility(View.INVISIBLE);
Log.e("student", "fav red fill : " + pos + "==" + position);
}
cursor.moveToNext();
}
holder.txt_name.setText(name[position]);
holder.iv_fruit.setImageResource(image[position]);
if (holder.iv_notFav.getVisibility() == View.VISIBLE) {
holder.iv_notFav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.iv_notFav.setVisibility(View.INVISIBLE);
holder.iv_Fav.setVisibility(View.VISIBLE);
//insert data...
String pos = String.valueOf(position);
myDatabase.insertData(pos, name[position], image[position]);
Toast.makeText(context, "Add to favourite!", Toast.LENGTH_SHORT).show();
}
});
} else if (holder.iv_Fav.getVisibility() == View.VISIBLE) {
holder.iv_Fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.iv_Fav.setVisibility(View.INVISIBLE);
holder.iv_notFav.setVisibility(View.VISIBLE);
//remove data...
myDatabase.removeData(String.valueOf(position));
Toast.makeText(context, "Remove from favourite!", Toast.LENGTH_SHORT).show();
}
});
}
// holder.iv_notFav.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
//
// if (!isFav) {
// holder.iv_fav.setImageResource(R.drawable.fav_fill);
// isFav = true;
// String pos = String.valueOf(position);
// myDatabase.insertData(pos, name[position]);
// Toast.makeText(context, "Add to favourite!", Toast.LENGTH_SHORT).show();
//
// } else {
// myDatabase.removeData(String.valueOf(position));
// holder.iv_fav.setImageResource(R.drawable.fav_line);
// isFav = false;
// Toast.makeText(context, "Remove from favourite!", Toast.LENGTH_SHORT).show();
// }
// }
// });
}
@Override
public int getItemCount() {
return name.length;
}
public class DataHolder extends RecyclerView.ViewHolder {
private TextView txt_name;
private ImageView iv_notFav;
private ImageView iv_Fav;
private ImageView iv_fruit;
public DataHolder(@NonNull View itemView) {
super(itemView);
txt_name = itemView.findViewById(R.id.txt_name);
iv_notFav = itemView.findViewById(R.id.iv_notFav);
iv_Fav = itemView.findViewById(R.id.iv_Fav);
iv_fruit = itemView.findViewById(R.id.iv_fruit);
}
}
}
STEP 12: JAVA of Fav Adapter
public class FavAdapter extends RecyclerView.Adapter<FavAdapter.DataHolder> {
Context context;
List<ModelFavList> dataList;
LayoutInflater inflater;
// boolean isFav = true;
MyDatabase myDatabase;
public FavAdapter(FavListActivity dashBordActivity, List<ModelFavList> dataList) {
context = dashBordActivity;
this.dataList = dataList;
inflater = LayoutInflater.from(context);
myDatabase = new MyDatabase(context);
}
@NonNull
@Override
public DataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_fav, parent, false);
return new DataHolder(view);
}
public void clearRecyclerView() {
int size = this.dataList.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
dataList.remove(0);
}
this.notifyItemRangeRemoved(0, size);
}
}
@Override
public void onBindViewHolder(@NonNull DataHolder holder, int position) {
holder.txt_name.setText(dataList.get(position).getName());
holder.iv_fruit.setImageResource(dataList.get(position).getImage());
holder.iv_fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pos = dataList.get(position).getPos();
myDatabase.removeData(pos);
Toast.makeText(context, "Remove from favourite!", Toast.LENGTH_SHORT).show();
((FavListActivity) context).refreshData();
}
});
}
@Override
public int getItemCount() {
return dataList.size();
}
public class DataHolder extends RecyclerView.ViewHolder {
private TextView txt_name;
private ImageView iv_fav;
private ImageView iv_fruit;
public DataHolder(@NonNull View itemView) {
super(itemView);
txt_name = itemView.findViewById(R.id.txt_name);
iv_fav = itemView.findViewById(R.id.iv_fav);
iv_fruit = itemView.findViewById(R.id.iv_fruit);
}
}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
tools:context=".Activity.MainActivity">
<Button
android:id="@+id/btn_dashboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="DASH BOARD" />
<Button
android:id="@+id/btn_fav_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="FAVOURITE LIST" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.DashBordActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.FavListActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_fruit"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/app_name"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_notFav"
app:layout_constraintStart_toEndOf="@+id/iv_fruit"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_notFav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/fav_line"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_Fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/fav_fill"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#696969"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_fruit"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/ic_launcher_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/app_name"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_fav"
app:layout_constraintStart_toEndOf="@+id/iv_fruit"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/iv_fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?selectableItemBackground"
android:src="@drawable/fav_fill"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#696969"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MyDatabase extends SQLiteOpenHelper {
public MyDatabase(@Nullable Context context) {
super(context, "mydb.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "create table bookmark(pos integer primary key, name text, image int)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
String query = "drop table if exists bookmark";
db.execSQL(query);
}
public void insertData(String pos, String name, int image) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("pos", pos);
contentValues.put("name", name);
contentValues.put("image", image);
database.insert("bookmark", null, contentValues);
}
public void removeData(String pos) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("pos", pos);
long res = database.delete("bookmark", "pos=?", new String[]{pos});
if (res == 1) {
Log.e("student", "removeData: success" + res);
} else {
Log.e("student", "removeData: failed!..........................." + res);
}
}
public Cursor getData() {
SQLiteDatabase database = getReadableDatabase();
String query = "select * from bookmark";
Cursor cursor = database.rawQuery(query, null);
return cursor;
}
}
public class ModelFavList {
String pos;
String name;
int image;
public ModelFavList(String pos, String name, int image) {
this.pos = pos;
this.name = name;
this.image = image;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getPos() {
return pos;
}
public void setPos(String pos) {
this.pos = pos;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class MainActivity extends AppCompatActivity {
private Button btn_dashboard, btn_fav_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_dashboard = findViewById(R.id.btn_dashboard);
btn_fav_list = findViewById(R.id.btn_fav_list);
btn_dashboard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DashBordActivity.class));
}
});
btn_fav_list.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, FavListActivity.class));
}
});
}
}
public class DashBordActivity extends AppCompatActivity {
private DashbordAdapter dashbordAdapter;
private RecyclerView rv;
private String[] name = {"Apple", "Banana", "Cherry", "Grapes", "Kiwi", "Mango", "Orange", "Papaya", "Pineapple", "Strawberry", "Watermelone"};
private int[] image = {
R.drawable.fruit_apple,
R.drawable.fruit_banana,
R.drawable.fruit_cherry,
R.drawable.fruit_grapes,
R.drawable.fruit_kiwi,
R.drawable.fruit_mango,
R.drawable.fruit_orange,
R.drawable.fruit_papaya,
R.drawable.fruit_pineapple,
R.drawable.fruit_strawberry,
R.drawable.fruit_watermelon,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_bord);
rv = findViewById(R.id.rv);
dashbordAdapter = new DashbordAdapter(this, name, image);
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
rv.setLayoutManager(manager);
rv.setAdapter(dashbordAdapter);
}
}
public class FavListActivity extends AppCompatActivity {
MyDatabase myDatabase;
RecyclerView recyclerView;
List<ModelFavList> dataList = new ArrayList<>();
FavAdapter favAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fav_list);
recyclerView = findViewById(R.id.recyclerView);
myDatabase = new MyDatabase(this);
favAdapter = new FavAdapter(this, dataList);
refreshData();
}
public void refreshData() {
favAdapter.clearRecyclerView();
Cursor cursor = myDatabase.getData();
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String pos = cursor.getString(0);
String name = cursor.getString(1);
int image = cursor.getInt(2);
ModelFavList modelFavList = new ModelFavList(pos, name, image);
dataList.add(modelFavList);
cursor.moveToNext();
}
RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(favAdapter);
}
}
public class DashbordAdapter extends RecyclerView.Adapter<DashbordAdapter.DataHolder> {
Context context;
String[] name;
int[] image;
LayoutInflater inflater;
//boolean isFav = false;
MyDatabase myDatabase;
public DashbordAdapter(DashBordActivity dashBordActivity, String[] name, int[] image) {
context = dashBordActivity;
this.name = name;
this.image=image;
inflater = LayoutInflater.from(context);
myDatabase = new MyDatabase(context);
}
@NonNull
@Override
public DataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_dashbord, parent, false);
return new DataHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DataHolder holder, int position) {
//TODO: open fav listed data...
Cursor cursor = myDatabase.getData();
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
String pos = cursor.getString(0);
String name = cursor.getString(1);
String image = cursor.getString(2);
if (pos.equals(String.valueOf(position))) {
// holder.iv_fav.setImageResource(R.drawable.fav_fill);
holder.iv_Fav.setVisibility(View.VISIBLE);
holder.iv_notFav.setVisibility(View.INVISIBLE);
Log.e("student", "fav red fill : " + pos + "==" + position);
}
cursor.moveToNext();
}
holder.txt_name.setText(name[position]);
holder.iv_fruit.setImageResource(image[position]);
if (holder.iv_notFav.getVisibility() == View.VISIBLE) {
holder.iv_notFav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.iv_notFav.setVisibility(View.INVISIBLE);
holder.iv_Fav.setVisibility(View.VISIBLE);
//insert data...
String pos = String.valueOf(position);
myDatabase.insertData(pos, name[position], image[position]);
Toast.makeText(context, "Add to favourite!", Toast.LENGTH_SHORT).show();
}
});
} else if (holder.iv_Fav.getVisibility() == View.VISIBLE) {
holder.iv_Fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.iv_Fav.setVisibility(View.INVISIBLE);
holder.iv_notFav.setVisibility(View.VISIBLE);
//remove data...
myDatabase.removeData(String.valueOf(position));
Toast.makeText(context, "Remove from favourite!", Toast.LENGTH_SHORT).show();
}
});
}
// holder.iv_notFav.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
//
// if (!isFav) {
// holder.iv_fav.setImageResource(R.drawable.fav_fill);
// isFav = true;
// String pos = String.valueOf(position);
// myDatabase.insertData(pos, name[position]);
// Toast.makeText(context, "Add to favourite!", Toast.LENGTH_SHORT).show();
//
// } else {
// myDatabase.removeData(String.valueOf(position));
// holder.iv_fav.setImageResource(R.drawable.fav_line);
// isFav = false;
// Toast.makeText(context, "Remove from favourite!", Toast.LENGTH_SHORT).show();
// }
// }
// });
}
@Override
public int getItemCount() {
return name.length;
}
public class DataHolder extends RecyclerView.ViewHolder {
private TextView txt_name;
private ImageView iv_notFav;
private ImageView iv_Fav;
private ImageView iv_fruit;
public DataHolder(@NonNull View itemView) {
super(itemView);
txt_name = itemView.findViewById(R.id.txt_name);
iv_notFav = itemView.findViewById(R.id.iv_notFav);
iv_Fav = itemView.findViewById(R.id.iv_Fav);
iv_fruit = itemView.findViewById(R.id.iv_fruit);
}
}
}
public class FavAdapter extends RecyclerView.Adapter<FavAdapter.DataHolder> {
Context context;
List<ModelFavList> dataList;
LayoutInflater inflater;
// boolean isFav = true;
MyDatabase myDatabase;
public FavAdapter(FavListActivity dashBordActivity, List<ModelFavList> dataList) {
context = dashBordActivity;
this.dataList = dataList;
inflater = LayoutInflater.from(context);
myDatabase = new MyDatabase(context);
}
@NonNull
@Override
public DataHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_fav, parent, false);
return new DataHolder(view);
}
public void clearRecyclerView() {
int size = this.dataList.size();
if (size > 0) {
for (int i = 0; i < size; i++) {
dataList.remove(0);
}
this.notifyItemRangeRemoved(0, size);
}
}
@Override
public void onBindViewHolder(@NonNull DataHolder holder, int position) {
holder.txt_name.setText(dataList.get(position).getName());
holder.iv_fruit.setImageResource(dataList.get(position).getImage());
holder.iv_fav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pos = dataList.get(position).getPos();
myDatabase.removeData(pos);
Toast.makeText(context, "Remove from favourite!", Toast.LENGTH_SHORT).show();
((FavListActivity) context).refreshData();
}
});
}
@Override
public int getItemCount() {
return dataList.size();
}
public class DataHolder extends RecyclerView.ViewHolder {
private TextView txt_name;
private ImageView iv_fav;
private ImageView iv_fruit;
public DataHolder(@NonNull View itemView) {
super(itemView);
txt_name = itemView.findViewById(R.id.txt_name);
iv_fav = itemView.findViewById(R.id.iv_fav);
iv_fruit = itemView.findViewById(R.id.iv_fruit);
}
}
}