【发布时间】:2021-03-29 18:47:56
【问题描述】:
我在数据库中有一个“服务”表,我使用适配器类在复选框中选择服务来获取服务。它显示带有复选框的服务。我想下客户通过复选框选择的订单。我需要将选中的复选框数据传递给 Api。但是如何将多个选中的复选框值传递给服务器?帮帮我。
这是我的代码
<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);
我试过上面的代码。 Service.java 类我得到所有服务响应以显示在 recyclerview 中并带有一个复选框。现在我想将选中的复选框值传递给服务器。请帮帮我。
【问题讨论】: