Lê Khánh Vinh Lê Khánh Vinh - 3 months ago 8
Android Question

Allow user to select only 1 item in Recycle View Android

Hi I have a

RecycleView
and an adapter with
CheckBox
that allow user to select multiple item in the Recycle view (checked item will be store in an ArrayList) Now I want to add another function that allow user to only choose 1 item in the Recycle view (only 1 checkbox is checked) (User can change the selection freely). How to implement this. Any help is much appreciate

Here is my
apdater
(for select multiple item) (I want to modify or add another adpater to allow user to select only 1 item, only 1 checkbox is checked at a time)

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

private Context context;
List <Event> events, eventSelected;

public EventAdapterAssign(Context context, List<Event> events, List<Event> eventSelected) {
this.context = context;
this.events = events;
this.eventSelected = eventSelected;
}


@Override
public EventAdapterAssign.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);

// Inflate the custom layout
View eventView1 = inflater.inflate(R.layout.event_list_assign, parent, false);

// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(eventView1);
return viewHolder;
}

@Override
public void onBindViewHolder(EventAdapterAssign.ViewHolder holder, int position) {
//Getting the particular item from the list
final Event event = events.get(position);

//Showing data on the views
holder.EventTitle.setHint(event.getEventTitle());
holder.EventDesc.setText(event.getEventDesc());
holder.EventStartTime.setText(event.getEventStartTime());
holder.EventEndTime.setText(event.getEventEndTime());
holder.EventID.setText(event.getEventID());

//in some case, it will prevent unwanted situations;
holder.selectCheckBox.setOnCheckedChangeListener(null);

//if true, your check box will be selected, else unselected
holder.selectCheckBox.setChecked(event.isSelected());

holder.selectCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

event.setIsSelected(isChecked);
if (isChecked) {
eventSelected.add(event);
} else {
eventSelected.remove(event);
}
}
});

}

// Return the total count of items
@Override
public int getItemCount() {
return events.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
//View
public EditText EventTitle;
public TextView EventDesc;
public TextView EventStartTime;
public TextView EventEndTime;
public TextView EventID;
public CheckBox selectCheckBox;

//initiating View
public ViewHolder(View itemView) {
super(itemView);

// display Check In event when beacon in range

EventTitle = (EditText)itemView.findViewById(R.id.EventTitle);
EventDesc = (TextView)itemView.findViewById(R.id.EventDesc);
EventStartTime = (TextView)itemView.findViewById(R.id.EventStartTime);
EventEndTime = (TextView)itemView.findViewById(R.id.EventEndTime);
EventID = (TextView)itemView.findViewById(R.id.EventID);

selectCheckBox = (CheckBox)itemView.findViewById(R.id.selectCheckBox);

}
}


and here is my data model (Event) class

public class Event implements Parcelable {
private String EventID;
private String EventDesc;
private String EventTitle;
private String EventStartTime;
private String EventEndTime;

private Context context;
private boolean isSelected;

public boolean isSelected() {
return isSelected;
}

public void setIsSelected(boolean isSelected) {
this.isSelected = isSelected;
}

public String getEventID() {
return EventID;
}

public void setEventID(String eventID) {
EventID = eventID;
}

public String getEventDesc() {
return EventDesc;
}

public void setEventDesc(String eventDesc) {
EventDesc = eventDesc;
}

public String getEventTitle() {
return EventTitle;
}

public void setEventTitle(String eventTitle) {
EventTitle = eventTitle;
}

public String getEventStartTime() {
return EventStartTime;
}

public void setEventStartTime(String eventStartTime) {
EventStartTime = eventStartTime;
}

public String getEventEndTime() {
return EventEndTime;
}

public void setEventEndTime(String eventEndTime) {
EventEndTime = eventEndTime;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.EventID);
dest.writeString(this.EventDesc);
dest.writeString(this.EventTitle);
dest.writeString(this.EventStartTime);
dest.writeString(this.EventEndTime);
}

public Event() {
}

protected Event(Parcel in) {
this.EventID = in.readString();
this.EventDesc = in.readString();
this.EventTitle = in.readString();
this.EventStartTime = in.readString();
this.EventEndTime = in.readString();
}

public static final Parcelable.Creator<Event> CREATOR = new Parcelable.Creator<Event>() {
public Event createFromParcel(Parcel source) {
return new Event(source);
}

public Event[] newArray(int size) {
return new Event[size];
}
};


}

Answer
    public class EventAdapterAssign extends RecyclerView.Adapter<EventAdapterAssign.ViewHolder> {

     private Context context;
            List <Event> events, eventSelected;
                private SparseBooleanArray mSparseBooleanArray;
        public  int selectedPosition;
            public EventAdapterAssign(Context context, List<Event> events, List<Event> eventSelected) {
                this.context = context;
                this.events = events;
                this.eventSelected = eventSelected;
        mSparseBooleanArray = new SparseBooleanArray();    
        }


            @Override
            public EventAdapterAssign.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                Context context = parent.getContext();
                LayoutInflater inflater = LayoutInflater.from(context);

                // Inflate the custom layout
                View eventView1 = inflater.inflate(R.layout.event_list_assign, parent, false);

                // Return a new holder instance
                ViewHolder viewHolder = new ViewHolder(eventView1);
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(EventAdapterAssign.ViewHolder holder, int position) {
                //Getting the particular item from the list
                 Event event = events.get(position);

                //Showing data on the views
                holder.EventTitle.setHint(event.getEventTitle());
                holder.EventDesc.setText(event.getEventDesc());
                holder.EventStartTime.setText(event.getEventStartTime());
                holder.EventEndTime.setText(event.getEventEndTime());
                holder.EventID.setText(event.getEventID());

                //in some case, it will prevent unwanted situations;
                holder.selectCheckBox.setOnCheckedChangeListener(null);

                //if true, your check box will be selected, else unselected

            holder.selectCheckBox.setTag(position);

            if(position==selectedPosition)
            {
            holder.selectCheckBox.setChecked(true);
            }
            else
            {
            holder.selectCheckBox.setChecked(false);
            }
                holder.selectCheckBox.setOnCheckedChangeListener(new CheckListener(holder.selectCheckBox,position));

            }

            // Return the total count of items
            @Override
            public int getItemCount() {
                return events.size();
            }

            class CheckListener implements OnCheckedChangeListener {

                    private CheckBox checkbox;
            Event data;
                    public CheckListener(CheckBox checkbox,int position) {

                        this.checkbox = checkbox;
                this.position=position;

                    }

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {

                        if (isChecked) {
                            checkbox.setChecked(true);
            selectedPosition = position;
   EventAdapterAssign.this.notifyDataSetChanged();
                        } else {
                            checkbox.setChecked(false);

                        }
                        buttonView.setChecked(isChecked);

                    }


                }

        public Event getCheckedItems() {


                return event.get(selectedPosition);
            }

            public class ViewHolder extends RecyclerView.ViewHolder {
                //View
                public EditText EventTitle;
                public TextView EventDesc;
                public TextView EventStartTime;
                public TextView EventEndTime;
                public TextView EventID;
                public CheckBox selectCheckBox;

                //initiating View
                public ViewHolder(View itemView) {
                    super(itemView);

                    // display Check In event when beacon in range

                    EventTitle = (EditText)itemView.findViewById(R.id.EventTitle);
                    EventDesc = (TextView)itemView.findViewById(R.id.EventDesc);
                    EventStartTime = (TextView)itemView.findViewById(R.id.EventStartTime);
                    EventEndTime = (TextView)itemView.findViewById(R.id.EventEndTime);
                    EventID = (TextView)itemView.findViewById(R.id.EventID);

                    selectCheckBox = (CheckBox)itemView.findViewById(R.id.selectCheckBox);

                }
            }
Comments