I want to create this card view inside recycler View.
I got this problem.I can't retrieve the data inside of the card views.
I want to whenever that Check Box is true, Get the data(Meal, price, and count which is in this example (1)) and add to the list in my Main Activity.
I am stuck on this for 6 hour straight. If someone know the solution please help. I am dying over here.
MainActivity:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
CardData data;
List<CardData> list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Hawk.init(getApplicationContext()).build();
data = new CardData("Meal","1","12","http");
list.add(data);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
final RecyclerView.Adapter adapter=new MyAdapter(getApplicationContext(),list);
recyclerView.setAdapter(adapter);
}
}
Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<CardData> data;
private Context context;
public MyAdapter(Context context,List<CardData> data){
this.data=data;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_contents,viewGroup,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(final MyViewHolder myViewHolder, final int i) {
myViewHolder.Meal.setText(data.get(i).Yemek);
myViewHolder.price.setText(data.get(i).Qiymet);
//Glide.with(context).load(data.get(i).url).into(myViewHolder.img);
myViewHolder.count.getText();
myViewHolder.card.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myViewHolder.checkBox.setChecked(!myViewHolder.checkBox.isChecked());
data.get(i).checked = myViewHolder.checkBox.isChecked();
}
});
myViewHolder.add.setFocusable(true);
myViewHolder.remove.setFocusable(true);
myViewHolder.add.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String countS = myViewHolder.count.getText().toString();
int countI = Integer.valueOf(countS);
countI += 1;
String countN = String.valueOf(countI);
myViewHolder.count.setText(countN);
}
});
myViewHolder.remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String sayS = myViewHolder.count.getText().toString();
int sayI = Integer.valueOf(sayS);
if (sayI>0){
sayI -= 1;}
String sayN = String.valueOf(sayI);
myViewHolder.count.setText(sayN);
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView Meal;
TextView price;
TextView count;
CardView card;
ImageView img;
CheckBox checkBox;
Button add;
Button remove;
MyViewHolder(View view){
super(view);
this.Meal = (TextView) view.findViewById(R.id.Meal);
this.price = (TextView) view.findViewById(R.id.Cost);
this.count = (TextView) view.findViewById(R.id.Count);
this.add = (Button) view.findViewById(R.id.addButton);
this.remove = (Button) view.findViewById(R.id.removeButton);
this.checkBox = (CheckBox) view.findViewById(R.id.checkBox);
this.img = (ImageView) view.findViewById(R.id.img);
this.card = (CardView) view.findViewById(R.id.card_view);
}
}
}
Data:
public class CardData {
public CardData() {
}
public String Meal;
public String Portion;
public String Cost;
public String url;
public Boolean checked;
public CardData(String meal, String portion, String cost, String url) {
Meal = meal;
Portion = portion;
Cost = cost;
this.url = url;
}
public Boolean isChecked() {
return checked;
}
public void setChecked(Boolean checked) {
this.checked = checked;
}
public String getMeal() {
return Meal;
}
public void setMeal(String meal) {
Meal = meal;
}
public String getPortion() {
return Portion;
}
public void setPortion(String portion) {
Portion = portion;
}
public String getCost() {
return Cost;
}
public void setCost(String cost) {
Cost = cost;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}