mardi 3 janvier 2017

Android cardview onclick implentation

I had make a list of cardviews that are series of time starting from 0:00 to 23:00 with interval of 15 minutes, that is [0:00 , 0:15, ...., 22:45, 23:00] now I want to implement onClick to the cards and whenever I click on one card there will be a checkbox set in the card with checked set as true, and when I click on another card the previous clicked card checkbox will be set as false and invisible while the new one will contain a checked checkbox

 public class SuggestTimeCardAdapter extends RecyclerView.Adapter<SuggestTimeCardAdapter.ViewHolder> {

private Context context;

List<SuggestTimeList> suggestTimeLists;

public SuggestTimeCardAdapter(List<SuggestTimeList> suggestTimeLists, Context context){
    super();
    this.suggestTimeLists = suggestTimeLists;
    this.context = context;
}

@Override
public SuggestTimeCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.suggest_time_card, parent, false);
    SuggestTimeCardAdapter.ViewHolder viewHolder = new SuggestTimeCardAdapter.ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(final SuggestTimeCardAdapter.ViewHolder holder, int position) {

    SuggestTimeList suggestTimeList =  suggestTimeLists.get(position);

    holder.tvSuggestTime.setText(suggestTimeList.getSuggestTime());

    holder.suggest_time_card_view.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v){
            //implement the click
            holder.cbSuggestTime.setVisibility(View.VISIBLE);
            holder.cbSuggestTime.setChecked(true);
        }
    });


}
@Override
public int getItemCount() {
    return suggestTimeLists.size();
}

class ViewHolder extends RecyclerView.ViewHolder {
    public TextView tvSuggestTime;
    public CheckBox cbSuggestTime;
    public CardView suggest_time_card_view;


    public ViewHolder(View itemView) {
        super(itemView);
        tvSuggestTime = (TextView) itemView.findViewById(R.id.tvSuggestTime);
        cbSuggestTime = (CheckBox) itemView.findViewById(R.id.cbSuggestTime);
        suggest_time_card_view = (CardView) itemView.findViewById(R.id.suggest_time_card_view);


    }
}

}

Now the problem I have is that whenever I click on one card, for example 0:00, then all cards with increment of 2:15 will be checked, so that 2:15 , 4:30 etc. are all checked, then while scrolling and not clicking anything the checked cards will suddenly become unchecked and random cards are checked. I am confused here , can someone please help?




Aucun commentaire:

Enregistrer un commentaire