I have a recyclerview with checkboxes. The data in recyclerview is from backend. When i check multiple checkboxes in recyclerview the positions of that checkboxes need to set to one string with comma seperation.
My recyclerview items coming from this retrofit call.I'm using retrofit2 for network call.
public void facilitiesList(){
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<Object> call = apiInterface.getHostelFacilities();
ApiRequest.getInstance().serviceCalls(getActivity(), call, new ApiRequest.ServiceCallBack() {
@Override
public void successful(Response response) {
Gson gson = new Gson();
String target = gson.toJson(response.body());
HostelFacilitiesResponseModel responseModel = gson.fromJson(target, HostelFacilitiesResponseModel.class);
if (responseModel.getStatusCode() == 1){
facilitiesModelArrayList = responseModel.getData();
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false));
adapter = new FatiesAdapter(facilitiesModelArrayList);
recyclerView.setAdapter(adapter);
for (int i=0; i< facilitiesModelArrayList.size(); i++){
HostelFacilitiesModel facilitiesModel = facilitiesModelArrayList.get(i);
if (facilitiesModel.isSelected()){
itemsChecked = itemsChecked + "," + facilitiesModel.getId();
}
}
Toast.makeText(getActivity(), "Items Checked: "+itemsChecked, Toast.LENGTH_SHORT).show();
}
}
@Override
public void fail(Throwable t) {
}
});
}
MyAdapter
public class FatiesAdapter extends RecyclerView.Adapter <FatiesAdapter.FactiesHolder >{
ArrayList<FatiesModel> fatiesModelArrayList;
public FatiesAdapter (ArrayList<FatiesModel> fatiesModelArrayList) {
this.fatiesModelArrayList= fatiesModelArrayList;
}
@NonNull
@Override
public FactiesHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_hostel_facility, parent, false);
return new FactiesHolder (view);
}
@Override
public void onBindViewHolder(@NonNull FactiesHolder holder, int position) {
FatiesModel factiesModel = fatiesModelArrayList.get(position);
holder.Name.setText(factiesModel.getName());
holder.checkBox.setChecked(factiesModel.isSelected());
}
@Override
public int getItemCount() {
return factiesModelArrayList.size();
}
public class FactiesHolder extends RecyclerView.ViewHolder {
TextView Name;
CheckBox checkBox;
public FactiesHolder (@NonNull View itemView) {
super(itemView);
Name = itemView.findViewById(R.id.tvName);
checkBox = itemView.findViewById(R.id.cb);
}
}
}
Aucun commentaire:
Enregistrer un commentaire