如何在android中使用api将多个选中的复选框值传递给服务器?
Posted
技术标签:
【中文标题】如何在android中使用api将多个选中的复选框值传递给服务器?【英文标题】:How to pass Multiple selected checkbox value to the server using api in android? 【发布时间】:2021-03-29 18:47:56 【问题描述】:我在数据库中有一个“服务”表,我使用适配器类在复选框中选择服务来获取服务。它显示带有复选框的服务。我想下客户通过复选框选择的订单。我需要将选中的复选框数据传递给 Api。但是如何将多个选中的复选框值传递给服务器?帮帮我。
这是我的代码
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_
android:layout_
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_
android:layout_
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 中并带有一个复选框。现在我想将选中的复选框值传递给服务器。请帮帮我。
【问题讨论】:
【参考方案1】:在您的 adapter 类中,您将 position 添加到列表 mSelectedItemIds。
mSelectedItemsIds.add(position);
您应该做的是将Service
类的对象中的字段添加到该列表中。因此,假设您要将所选复选框的laundryServiceId
s 传递给服务器。那么,在这种情况下,你应该这样做:
在您的适配器中,将列表项的类型更改为字符串:
private static List<Integer> mSelectedItemsIds;
在您的适配器的onBindViewHolder
中:
if(isChecked)
mSelectedItemsIds.add(serviceClass.getLaundryServiceId());
else
mSelectedItemsIds.remove(serviceClass.getLaundryServiceId());
然后在需要时将ServiceAdapter.getSelectedIds()
传递给服务器。
我希望这能解决您的问题,如果有任何困惑,请随时发表评论。
【讨论】:
非常感谢,先生。有用。如何在 php 文件中获取这些 id。我尝试访问,但显示错误 Use JsonReader.setLenient(true) to accept malformed JSON at line 1 Column 1 path $ 我不懂php。但是,由于现在您正在传递一个字符串列表(因为 getLaundryServiceId 返回字符串),您需要在两端修改您的 API。 还有其他方法可以将列表发送到服务器吗?以上是关于如何在android中使用api将多个选中的复选框值传递给服务器?的主要内容,如果未能解决你的问题,请参考以下文章
使用android中的自定义arrayadapter在listview中使用复选框检查后如何删除多个项目?
在 Rails 中,如何处理多个选中的复选框,只在 , 或?
asp.net中radiobuttonlist使用控件,如何将多个选中的选项转化为数组并输出。