I have a "Service" table in the database and I get the services using adapter class for service selection in the checkbox. It shows the service with a checkbox. I want to place the order which is selected by customer through checkbox. I need to pass the selected checkbox data to Api. But how to pass multiple checked checkbox values to the server?? Help me.
Here is my code
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_get"
android:layout_width="91dp"
android:layout_height="38dp"
android:text="Place Order"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/recycler_view"
app:layout_constraintStart_toStartOf="@+id/recycler_view"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.72"
tools:ignore="MissingConstraints" />
Service.java
public class Service {
@SerializedName("laundry_service_id")
@Expose
String laundryServiceId;
@SerializedName("laundry_service_name")
@Expose
String laundryServiceName;
@SerializedName("laundry_service_short_form")
@Expose
String laundryServiceShortForm;
@SerializedName("laundry_order_status_id")
@Expose
String laundryOrderStatusId;
@SerializedName("laundry_service_create_by")
@Expose
String laundryServiceCreateBy;
@SerializedName("laundry_service_join_date")
@Expose
String laundryServiceJoinDate;
public Service(String laundryServiceId, String laundryServiceName,
String laundryServiceShortForm, String laundryOrderStatusId,
String laundryServiceCreateBy, String laundryServiceJoinDate) {
this.laundryServiceId = laundryServiceId;
this.laundryServiceName = laundryServiceName;
this.laundryServiceShortForm = laundryServiceShortForm;
this.laundryOrderStatusId = laundryOrderStatusId;
this.laundryServiceCreateBy = laundryServiceCreateBy;
this.laundryServiceJoinDate = laundryServiceJoinDate;
}
public String getLaundryServiceId() {
return laundryServiceId;
}
public void setLaundryServiceId(String laundryServiceId) {
this.laundryServiceId = laundryServiceId;
}
public String getLaundryServiceName() {
return laundryServiceName;
}
public void setLaundryServiceName(String laundryServiceName) {
this.laundryServiceName = laundryServiceName;
}
public String getLaundryServiceShortForm() {
return laundryServiceShortForm;
}
public void setLaundryServiceShortForm(String laundryServiceShortForm) {
this.laundryServiceShortForm = laundryServiceShortForm;
}
public String getLaundryOrderStatusId() {
return laundryOrderStatusId;
}
public void setLaundryOrderStatusId(String laundryOrderStatusId) {
this.laundryOrderStatusId = laundryOrderStatusId;
}
public String getLaundryServiceCreateBy() {
return laundryServiceCreateBy;
}
public void setLaundryServiceCreateBy(String laundryServiceCreateBy) {
this.laundryServiceCreateBy = laundryServiceCreateBy;
}
public String getLaundryServiceJoinDate() {
return laundryServiceJoinDate;
}
public void setLaundryServiceJoinDate(String laundryServiceJoinDate) {
this.laundryServiceJoinDate = laundryServiceJoinDate;
}
}
ServiceAdapter.java
public class ServiceAdapter extends RecyclerView.Adapter<ServiceAdapter.ViewHolder> {
private static List<Integer> mSelectedItemsIds;
List<Service> serviceList;
//private List<Integer> mSelectedItemsIds;
Context context;
public ServiceAdapter(List<Service> serviceList, Context context) {
this.serviceList = serviceList;
this.context = context;
mSelectedItemsIds = new ArrayList<>();
}
public static List<Integer> getSelectedIds() {
return mSelectedItemsIds;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.service_layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
final Service serviceClass = serviceList.get(position);
holder.chkbox.setText(serviceClass.getLaundryServiceName());
holder.chkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
mSelectedItemsIds.add(position);
}else{
mSelectedItemsIds.remove(position);
}
}
});
}
@Override
public int getItemCount() {
return serviceList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CheckBox chkbox;
public ViewHolder(@NonNull View itemView) {
super(itemView);
chkbox=itemView.findViewById(R.id.chkbox);
}
}
}
CheckboxActivity.java
public class CheckboxActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Service> serList;
CheckBox checkBox;
int id;
Button btn_get;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
checkBox = findViewById(R.id.chkbox);
btn_get = findViewById(R.id.btn_get);
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
LinearLayoutManager LayoutManager = new LinearLayoutManager(CheckboxActivity.this,
LinearLayoutManager.VERTICAL,false);
recyclerView.setLayoutManager(LayoutManager);
btn_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewService();
}
});
Call<List<Service>>call = RetrofitClient
.getInstance()
.getApi()
.getServices();
call.enqueue(new Callback<List<Service>>() {
@Override
public void onResponse(Call<List<Service>> call, Response<List<Service>> response) {
serList = response.body();
ServiceAdapter serviceAdapter = new ServiceAdapter(serList,CheckboxActivity.this);
recyclerView.setAdapter(serviceAdapter);
id = serList.size();
Toast.makeText(CheckboxActivity.this,"Get the services"+id,Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<List<Service>> call, Throwable t) {
Toast.makeText(CheckboxActivity.this,"Try It" +
t.getMessage(),Toast.LENGTH_SHORT).show();
System.out.println(t.getMessage());
}
});
}
private void viewService() {
List<Integer> selectedid = ServiceAdapter.getSelectedIds();
//Log.d("List","=");
Toast.makeText(CheckboxActivity.this,"Clicked"+selectedid,Toast.LENGTH_SHORT).show();
for (int i = 0; i < selectedid.size(); i++) {
int chk = selectedid.get(i);
if (checkBox.isChecked()) {
String ch = checkBox.getText().toString();
Toast.makeText(CheckboxActivity.this,"Data"+ch,Toast.LENGTH_SHORT).show();
}
}
}
}
Api.java
@FormUrlEncoded
@POST("place_id.php")
Call<OrderResponse> place(
@Field("list") List<Integer> selectedid);
I tried the above code. Service.java class I get all service responses to show in recyclerview with a checkbox. Now I want to pass the selected checkbox value to the server. Please help me.