'How do I possibly create favorite list as I store & retrieve data in string using CData
In my own effort, I used SharedPreference to create memory for my savings. But I don't know how to finish it up and make it workable. I have already gone far and have saved my data in string using CData. But my app users insisted I should put favorite list where they can save lists of songs of their choices. I made my app using ListView with Adapter and when clicked opens the full detail of the song list in a new activity. I'm trying to create a favorite where a user has the option to click on favorite icon and save a list of song in the favorite list. In that same favorite list the user needs an option to either keep or delete the item from the favorite list.
This below is what I have been able to do that yielded no positive result. I don't know what to do next.
SharedPreference
public class PreferenceUtils {
private static final PreferenceUtils ourInstance = new PreferenceUtils();
private static SharedPreferences sharedPreferences;
private static Context context;
private static String APP_DATA_NAME = "SONGS";
static PreferenceUtils getInstance() {
return ourInstance;
}
private PreferenceUtils() {
}
public static void initialize(Context c) {
sharedPreferences = c.getSharedPreferences(APP_DATA_NAME, Context.MODE_PRIVATE);
context = c;
}
public static String getPreference(String key, String defaultValue) {
if (sharedPreferences == null) return null;
return sharedPreferences.getString(key, defaultValue);
}
public static boolean getPreference(String key, boolean defaultValue) {
if (sharedPreferences == null) return false;
return sharedPreferences.getBoolean(key, defaultValue);
}
public static Set<String> getPreference(String key) {
if (sharedPreferences == null) return null;
return sharedPreferences.getStringSet(key,null);
}
public static void setPreference(String key, String value) {
if (sharedPreferences == null) return;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
public static void setPreference(String key, boolean value) {
if (sharedPreferences == null) return;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
public static void setPreference(String key, Set<String> value) {
if (sharedPreferences == null) return;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet(key, value);
editor.apply();
}
}
Item_row
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="7dp"
app:cardElevation="4dp"
android:background="#F4F4F7"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/imageIv"
android:src="@drawable/cpu"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/titleTv"
android:text="Title"
android:textSize="17sp"
android:textColor="#000000"
android:fontFamily="sans-serif-condensed"
android:layout_toRightOf="@id/imageIv"
android:layout_toEndOf="@id/imageIv"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp" />
<TextView
android:id="@+id/descriptionTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/titleTv"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_toEndOf="@id/imageIv"
android:layout_toRightOf="@id/imageIv"
android:text="descriptionTv"
android:textColor="#DF014C"
android:textSize="11sp"
android:textStyle="italic" />
<ImageView
android:id="@+id/fav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="2dp"
android:src="@drawable/ic_star_outline"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
MyHolder
public class MyHolder1 extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView mImageView;
TextView mTitle, mDesc;
ItemClickListener1 itemCLickListener;
MyHolder1(@NonNull View itemView) {
super(itemView);
this.mImageView = itemView.findViewById(R.id.imageIv);
this.mTitle = itemView.findViewById(R.id.titleTv);
this.mDesc = itemView.findViewById(R.id.descriptionTv);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
this.itemCLickListener.onItemClickListener(v, getLayoutPosition());
}
public void setItemCLickListener(ItemClickListener1 ic){
this.itemCLickListener = ic;
}
}
MyAdapter1
public class MyAdapter1 extends RecyclerView.Adapter<MyHolder1> implements Filterable {
Context mContext;
ArrayList<Model1> models, filterList; // this array list create a list of array which parameter define in our class
CustomFilter1 filter;
public MyAdapter1(Context context, ArrayList<Model1> models) {
this.mContext = context;
this.models = models;
this.filterList = models;
}
@NonNull
@Override
public MyHolder1 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row1, null); //this line inflate our row
return new MyHolder1(view); //this will return our view to holder class
}
@Override
public void onBindViewHolder(@NonNull final MyHolder1 myHolder, int i) {
myHolder.mTitle.setText(models.get(i).getTitle()); //here is position
myHolder.mDesc.setText(models.get(i).getDesc());
myHolder.mImageView.setImageResource(models.get(i).getIcon()); // here we used imge resource
myHolder.setItemCLickListener(new ItemClickListener1() {
@Override
public void onItemClickListener(View v, int position) {
String gTitle = models.get(position).getTitle();
String gDesc = models.get(position).getDesc();
BitmapDrawable bitmapDrawable = (BitmapDrawable)myHolder.mImageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bytes = stream.toByteArray();
//get our data with intent
Intent intent = new Intent(mContext, NewActivity1.class);
intent.putExtra("actionBarTitle", models.get(position).getTitle());
intent.putExtra("brandNewDesc", models.get(position).getBrandNewDesc());
intent.putExtra("iImage", bytes);
mContext.startActivity(intent);
return;
}
});
}
@Override
public int getItemCount() {
return models.size();
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new CustomFilter1(filterList, this);
}
return filter;
}
}
Modal Class
public class Model1 {
private String title, description, desc;
private int icon;
String brandNewDesc;
//constructor
public Model1(String title, String desc, String description, int icon) {
this.title = title;
this.desc = desc;
this.icon = icon;
this.brandNewDesc = description;
}
//getters
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public String getBrandNewDesc() {
return brandNewDesc;
}
public int getIcon() {
return icon;
}
}
Example of data saved in String.xml
<string-array name="array_titles_1">
Mothering Sunday Wishes For my Mom
<item>"<![CDATA[I love my trip on trip track one]]>"</item>
<item>"<![CDATA[Back to my lover track two]]>"</item>
</string-array>
<string-array name="array_lyrics_1">
<item>"<![CDATA[description of song genre and sources]]>"</item>
<item>"<![CDATA[description of song genre and sources]]>"</item>
</string-array>
<string-array name="newbrandDesc_1">
<item>"<![CDATA[The new song detals
1. verse one
2. verse two....]]>"</item>
<item>"<![CDATA[The second song details
1. verse one
2. verse two....]]>"</item>
</string-array>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
