Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement checkbox in recyclerView?

I have an adapter for recyclerview. Where i pass array list from my activity like where data is ArrayList having some data.

 AlternativeAdapter alternativeAdapter= new AlternativeAdapter(AlternativeActivity.this, data);

    recyclerView.setAdapter(alternativeAdapter);

In the AlternativeAdapter class i have a textview and check box. I am able to display all text item with check boxes.

In the AlternativeAdapter class

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

 ArrayList<String> currencyListArray;
    Context context;
    View view1;
    ViewHolder viewHolder1;
    TextView textView;
    private int selectedPosition = -1;// no selection by default
    public AlternativeCurrencyAdapter(Context context1, ArrayList<String> currencyListArray){

        this.currencyListArray = currencyListArray;
        context = context1;
    }    
    public  class ViewHolder extends RecyclerView.ViewHolder{

        public TextView username;
        public CheckBox chkSelected;
        public ViewHolder(View v){
            super(v);
            username = (TextView)v.findViewById(R.id.username);
            chkSelected = (CheckBox) v.findViewById(R.id.chkSelected);
            itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    // get position
                    int pos = getAdapterPosition();

                    // check if item still exists
                    if(pos != RecyclerView.NO_POSITION){
                        String clickedDataItem = currencyListArray.get(pos);
                        Toast.makeText(v.getContext(), "You clicked " + clickedDataItem, Toast.LENGTH_SHORT).show();

                    }
                }
            });

        }
    }

    @Override
    public AlternativeCurrencyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

        view1 = LayoutInflater.from(context).inflate(R.layout.alternative_currency_adapter_list,parent,false);

        viewHolder1 = new ViewHolder(view1);

        return viewHolder1;
    }

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

        holder.username.setText(currencyListArray.get(position));
    }
    @Override
    public int getItemCount(){
        return currencyListArray.size();
    }
}

By this way the item of array list is displaying but my need is at item should display at left corner and checkbox of each item should display at right corner . when some one clicks on check box toast message should come like you selected XYZ item. And only single selection is required not multi selection.

when i use

 holder.chkSelected.setChecked(currencyListArray.get(position).isSelected());

I shows compile time error. Can not resolve isSelected()

Please guide me.

like image 228
Gyan S Awasthi Avatar asked Nov 27 '17 14:11

Gyan S Awasthi


1 Answers

First thing, your currencyListArray is arraylist of string so it doesnot return for "isSelected()" sorry!

and now come to your requirement, you need to show message when any of checkbox is selected.

So code like this:

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

    holder.username.setText(currencyListArray.get(position));
    holder.chkSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            //do your toast programming here.
        }
    });
}

now come to your second requirement, for selecting only one item at a time: here is code for that::

 chkSelected.setChecked(position== selectedPosition);

        chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    selectedPosition =  position;
                }
                else{
                     selectedPosition = -1;
                }
                notifyDataSetChanged();
            }
        });
like image 137
Ankit Patidar Avatar answered Nov 03 '22 18:11

Ankit Patidar