I am trying to make an application that rates 8 different types of foods with simple rater and recyclerview. Pressing the FAB button just adds a random food with 0 rating. I have gotten most of it to work except the rating of the rating bar. When I scroll up or down, I lose the ratings I had. How can I store the ratings of food when I scroll up or down.
RecyclerView:
package com.mycompany.alawamhm.foodrater;
import android.content.Context;
import android.graphics.Movie;
import android.icu.text.AlphabeticIndex;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import javax.xml.transform.sax.SAXSource;
public class FoodAdapter extends RecyclerView.Adapter<FoodAdapter.ViewHolder>{
private Random mRandom = new Random();
private RecyclerView mRecyclerView;
final HashMap<String,Integer> defaultName;
final ArrayList<String> mNames=new ArrayList<>();
public FoodAdapter(RecyclerView rv){
defaultName = new HashMap<>();
defaultName.put("banana", R.drawable.banana);
defaultName.put("broccoli", R.drawable.broccoli);
defaultName.put("homemade bread", R.drawable.bread);
defaultName.put("chicken", R.drawable.chicken);
defaultName.put("chocolate", R.drawable.chocolate);
defaultName.put("ice cream", R.drawable.icecream);
defaultName.put("lima beans", R.drawable.limabeans);
defaultName.put("steak", R.drawable.steak);
for(String name : defaultName.keySet()){
mNames.add(name);
}
mRecyclerView = rv;
}
public String getRandomName(){
String[] names = new String[]{
"banana","broccoli","homemade bread","chicken",
"chocolate","ice cream","lima beans","steak"
};
return names[mRandom.nextInt(names.length)];
}
public void addName(){
mNames.add(0,getRandomName());
notifyItemInserted(0);
mRecyclerView.getLayoutManager().scrollToPosition(0);
}
@Override
public FoodAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.name_view,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String name = mNames.get(position);
holder.mNameTextView.setText(name);
holder.mImage.setImageResource(defaultName.get(name));
holder.rBar.setRating(0);
}
@Override
public int getItemCount() {return mNames.size(); }
public void removeName(int position){
mNames.remove(position);
notifyItemRemoved(position);
}
class ViewHolder extends RecyclerView.ViewHolder{
private TextView mNameTextView;
private ImageView mImage;
SimpleRatingBar rBar;
public ViewHolder(View itemView) {
super(itemView);
mNameTextView = (TextView)itemView.findViewById(R.id.name_view);
mImage = (ImageView)itemView.findViewById(R.id.imageView);
rBar = (SimpleRatingBar)itemView.findViewById(R.id.ratingBar);
}
}
}
Currently the ratings bar is being set to 0 every time a view is bound to a view holder. Set a listener on the rating bar that runs a method to store the new rating value in a hashSet. Then in the adapter, instead of setting the rating bar to 0, add a helper method that iterates through the hashMap to check if a value has been stored for the food being inflated. This method should return the stored value or 0 if the food hasn't stored a value yet.