package com.example.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import com.example.aayushchaubey.meetdax.R;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.HashMap;
public class DifferentGenderServicesAdapter2 extends RecyclerView.Adapter {
private Task<QuerySnapshot> task;
String id, servicename;
ArrayList<String> serviceArray = new ArrayList<>();
ArrayList<Integer> costArray = new ArrayList<>();
ArrayList<ArrayList<String>> stylistIdArray = new ArrayList<>();
ArrayList<Integer>durationArray=new ArrayList<>();
TextView serviceTv, durationTv;
int count = 0;
String strService;
ArrayList<String> keyArr = new ArrayList<>();
ArrayList<String>serviceDocumentId=new ArrayList<>();
ArrayList<String>selectedServiceId=new ArrayList<>();
ArrayList<String>selectedServiceArray=new ArrayList<>();
ArrayList<Integer>selectedServiceCost=new ArrayList<>();
ArrayList<Integer>durationArr=new ArrayList<>();
String serviceId,strStylist,strDuration;
ArrayList<String>stylist=new ArrayList<>();
ArrayList<ArrayList<String>>selectedStylistId=new ArrayList<>();
public DifferentGenderServicesAdapter2(Task<QuerySnapshot> task, ArrayList<String> id, TextView servicesTv, TextView durationTv) {
this.task = task;
this.serviceTv = servicesTv;
this.durationTv = durationTv;
this.serviceDocumentId=id;
count = task.getResult().size();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.services_item, parent, false);
return new DifferentGenderServicesAdapter2.listViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
try {
((listViewHolder) holder).servicenameTv.setText(task.getResult().getDocuments().get(position).get("name").toString());
((listViewHolder) holder).serviceCost.setText(task.getResult().getDocuments().get(position).get("price").toString());
servicename = task.getResult().getDocuments().get(position).get("name").toString();
Integer price = task.getResult().getDocuments().get(position).getLong("price").intValue();
serviceArray.add(servicename);
costArray.add(price);
ArrayList<HashMap<String,String>>stylistArr=new ArrayList<>();
stylistArr=(ArrayList<HashMap<String, String>>)task.getResult().getDocuments().get(position).get("services");
ArrayList<String> stylistsId = new ArrayList<String>();
stylistsId.clear();
for (int i=0;i<stylistArr.size();i++){
strStylist=stylistArr.get(i).get("stylist").toString();
strDuration=stylistArr.get(i).get("duration").toString();
stylistsId.add(strStylist);
durationArray.add(Integer.parseInt(strDuration));
}
stylistIdArray.add(stylistsId);
((listViewHolder) holder).checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(((listViewHolder) holder).checkBox.isChecked()){
String service=serviceArray.get(position);
selectedServiceArray.add(service);
serviceTv.setText(selectedServiceArray.toString().replaceAll("\\[|\\]", ""));
}else {
selectedServiceArray.remove(position);
if (selectedServiceArray.size()== 0) {
serviceTv.setText("0");
} else {
serviceTv.setText(selectedServiceArray.toString());
}
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<String> getServiceId() {
return selectedServiceId;
}
public ArrayList<String> getServices() {
return selectedServiceArray;
}
public Integer getCostArray() {
Integer cost = null;
if(selectedServiceCost.size()>1){
cost=selectedServiceCost.get(0)+selectedServiceCost.get(1);
}else {
cost=selectedServiceCost.get(0);
}
return cost;
}
public Integer getDurationArr() {
Integer duration=null;
if(durationArr.size()>1){
duration=durationArr.get(0)+durationArr.get(1);
}else {
duration=durationArr.get(0);
}
return duration;
}
public ArrayList<ArrayList<String>> getSelectedStylistId() {
return selectedStylistId;
}
@Override
public int getItemCount() {
return count;
}
private class listViewHolder extends RecyclerView.ViewHolder {
private CheckBox checkBox;
private TextView servicenameTv, serviceCost;
int count = 0;
String strService;
public listViewHolder(final View itemView) {
super(itemView);
try {
servicenameTv = (TextView) itemView.findViewById(R.id.damerServiceTV);
serviceCost = (TextView) itemView.findViewById(R.id.damerRsTV);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这是我用来填充数组和checkbox.i想要为所选项目设置文本的recycleradap。如果我取消选择我要从textview中删除文本的项目。当我选择一个项目时,选择并取消选择正常工作。如果我有多个,或者如果我选择和去除倍数它的不工作指数错误
答案 0 :(得分:0)
这样做: -
if(checkbox.isChecked){
String service=serviceArray.get(getAdapterPosition)
selectedServiceArray.add(service);
holder.serviceNameTv.setText(selectedServiceArray.toString);
}
else{
selectedServiceArray.remove(getAdapterPosition);
if (selectedServiceArray.size = 0) {
holder.serviceNameTv.setText("0");
} else{
holder.serviceNameTv.setText(selectedServiceArray.toString);
}
答案 1 :(得分:0)
您正在尝试删除给定ReciclerView
选项中的项目,但这个位置错误,因为您正在执行此操作:
selectedServiceArray.add(service);
所以你不知道添加的位置,因为可能是这个数组中的其他项目。您必须请求删除对象而不是位置。试试这个:
if(checkbox.isChecked){
String service=serviceArray.get(getAdapterPosition);
selectedServiceArray.add(service);
holder.serviceNameTv.setText(selectedServiceArray.toString);
} else {
String service=serviceArray.get(getAdapterPosition);
selectedServiceArray.remove(service);
holder.serviceNameTv.setText(selectedServiceArray.toString);
}