如何获得另一个适配器内的第一个适配器位置以在SQLite数据库中设置值
我想从单选按钮onClick中选择一个值并将其保存到SQLite数据库 它工作正常,但需要onClick该项目的最后位置
所以我需要在内部适配器中的第一个适配器位置以在SQLite数据库中保存单选按钮的适当值
全球声明
int pos = 0;
这是我的第一个适配器代码
public class CartCustomAdapter extends RecyclerView.Adapter<CartCustomAdapter.MyViewHolder> {
private List<Cart> moviesList;
public CartCustomAdapter(List<Cart> moviesList) {
this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_cart_details, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final List<Cart> datum = moviesList;
pos = holder.getAdapterPosition();
Log.e("POSI1", pos + "");
if (loginModel != null) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("option_name", TextUtils.join(",", Collections.singleton(datum.get(position).getShippingOption() + "")));
hashMap.put("weight", datum.get(position).getWeight() + "");
hashMap.put("zip", loginModel.getResultLogin().getZip() + "");
Log.e("Parms", hashMap + "");
showProgressDialog();
Call<CheckOutShippingInfoModel> checkOutShippingInfoModelCall = RetrofitHelper.createService(RetrofitHelper.Service.class).CheckOutShippingInfoModel(hashMap);
checkOutShippingInfoModelCall.enqueue(new Callback<CheckOutShippingInfoModel>() {
@Override
public void onResponse(@NonNull Call<CheckOutShippingInfoModel> call, @NonNull Response<CheckOutShippingInfoModel> response) {
CheckOutShippingInfoModel object = response.body();
hideProgressDialog();
if (object != null && object.getError() == false) {
Log.e("TAG", "Shipping_Response : " + new Gson().toJson(response.body()));
holder.resultCheckoutShippingInfo = object.getResultCheckoutShippingInfo();
holder.resultCheckoutShippingInfo = object.getResultCheckoutShippingInfo();
holder.shippingCustomAdapter = new ShippingCustomAdapter(holder.resultCheckoutShippingInfo,
new ResultCallback() {
@Override
public void onItemClick(int position) {
//Do, what you need...
pos = holder.getAdapterPosition();
Log.e("postion", pos + "");
}
});
holder.recyclerViewShippingInfo.setAdapter(holder.shippingCustomAdapter);
} else {
}
}
@Override
public void onFailure(@NonNull Call<CheckOutShippingInfoModel> call, @NonNull Throwable t) {
hideProgressDialog();
t.printStackTrace();
Log.e("Shipping_Response", t.getMessage() + "");
}
});
} else {
Toast.makeText(getContext(), "Please Login", Toast.LENGTH_SHORT).show();
}
}
private int grandTotal() {
int totalPrice = 0;
for (int i = 0; i < moviesList.size(); i++) {
totalPrice += moviesList.get(i).getSubtotal();
// notifyDataSetChanged();
}
return totalPrice;
}
@Override
public int getItemCount() {
return moviesList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
RecyclerView recyclerViewShippingInfo;
private ShippingCustomAdapter shippingCustomAdapter;
private List<ResultCheckoutShippingInfo> resultCheckoutShippingInfo;
public MyViewHolder(View view) {
super(view);
recyclerViewShippingInfo = view.findViewById(R.id.recyclerViewShippingInfo);
recyclerViewShippingInfo.setLayoutManager(new GridLayoutManager(getContext(), 1));
recyclerViewShippingInfo.setHasFixedSize(true);
recyclerViewShippingInfo.setNestedScrollingEnabled(false);
}
}
}
这是第一个适配器内的另一个适配器代码
public class ShippingCustomAdapter extends RecyclerView.Adapter<ShippingCustomAdapter.MyViewHolder> {
private List<ResultCheckoutShippingInfo> moviesList;
private RadioGroup lastCheckedRadioGroup = null;
private int lastSelectedPosition = 0;
boolean isSelected = false;
int previousSelectedPosition = -1;
ResultCallback callback;
public ShippingCustomAdapter(List<ResultCheckoutShippingInfo> moviesList, ResultCallback callback) {
this.moviesList = moviesList;
this.callback = callback;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_shipping_info, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final List<ResultCheckoutShippingInfo> datum = moviesList;
Log.e("POSI", pos + "--" + position + "");
holder.shipping_name.setText(datum.get(position).getType() + "");
RadioButton rb = new RadioButton(getContext());
holder.radio.addView(rb);
if (cartId.equals("Standard")) {
rb.setChecked(true);
}
if (cartId.equals("Economy")) {
rb.setChecked(true);
}
if (cartId.equals("Free")) {
rb.setChecked(true);
}
}
@Override
public int getItemCount() {
return moviesList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView shipping_name, shipping_price;
RadioGroup radio;
RadioButton radioShipping;
public MyViewHolder(View view) {
super(view);
shipping_name = view.findViewById(R.id.shipping_name);
shipping_price = view.findViewById(R.id.shipping_price);
radio = view.findViewById(R.id.price_grp);
radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (lastCheckedRadioGroup != null
&& lastCheckedRadioGroup.getCheckedRadioButtonId()
!= radioGroup.getCheckedRadioButtonId()
&& lastCheckedRadioGroup.getCheckedRadioButtonId() != -1) {
lastCheckedRadioGroup.clearCheck();
databaseHelper.updateShippingInfo(cartModel.get(pos).getId(), shipping_price.getText().toString() + "", ShippingCustomAdapter.this.moviesList.get(i).getTypeId() + "");
label_subTotal.setText("Shipping : " + shipping_value + "\n" + "Total Amount : " + mSubTotal);
}
lastCheckedRadioGroup = radioGroup;
callback.onItemClick(i);
}
});
}
}
}
这是一个数据库查询,用于在单选按钮更改后更新值
public void updateShippingInfo(String id, String shipping, String current_option) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "UPDATE " + TABLE_NAME + " SET " + " shipping" + " = " + "'" + shipping + "'" + ", current_option" + " = " + "'" + current_option + "'" + " WHERE " + "id" + " = '" + id + "'";
Log.e("QUERY", sql);
db.execSQL(sql);
}
这是ResultCheckoutShippingInfo
public class ResultCheckoutShippingInfo {
@SerializedName("type")
@Expose
private String type;
@SerializedName("type_id")
@Expose
private String typeId;
@SerializedName("days")
@Expose
private String days;
@SerializedName("price")
@Expose
private String price;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTypeId() {
return typeId;
}
public void setTypeId(String typeId) {
this.typeId = typeId;
}
public String getDays() {
return days;
}
public void setDays(String days) {
this.days = days;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
这里我要更新数据库值 运费=价格 current_option = TypeId 使用其位置
答案 0 :(得分:1)
您需要创建:一个带有项目的RecyclerView.Adapter
,其中包含:照片,价格,+ | -按钮,删除按钮和radioGroup,它们具有动态radioButtons计数,由List<ResultCheckoutShippingInfo>
使用周期创建。
public class CartCustomAdapter extends RecyclerView.Adapter<CartCustomAdapter.MyViewHolder> {
private List<Cart> moviesList;
public CartCustomAdapter(List<Cart> moviesList) {
this.moviesList = moviesList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_cart_details, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
final List<Cart> datum = moviesList;
pos = holder.getAdapterPosition();
Log.e("POSI1", pos + "");
if (loginModel != null) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("option_name", TextUtils.join(",", Collections.singleton(datum.get(position).getShippingOption() + "")));
hashMap.put("weight", datum.get(position).getWeight() + "");
hashMap.put("zip", loginModel.getResultLogin().getZip() + "");
Log.e("Parms", hashMap + "");
showProgressDialog();
Call<CheckOutShippingInfoModel> checkOutShippingInfoModelCall = RetrofitHelper.createService(RetrofitHelper.Service.class).CheckOutShippingInfoModel(hashMap);
checkOutShippingInfoModelCall.enqueue(new Callback<CheckOutShippingInfoModel>() {
@Override
public void onResponse(@NonNull Call<CheckOutShippingInfoModel> call, @NonNull Response<CheckOutShippingInfoModel> response) {
CheckOutShippingInfoModel object = response.body();
hideProgressDialog();
if (object != null && object.getError() == false) {
Log.e("TAG", "Shipping_Response : " + new Gson().toJson(response.body()));
holder.resultCheckoutShippingInfo = object.getResultCheckoutShippingInfo();
//List<String> resultCheckoutShippingInfo = new ArrayList<>();
//resultCheckoutShippingInfo.add("Standard");
//resultCheckoutShippingInfo.add("Big cost");
for (ResultCheckoutShippingInfo info : holder.resultCheckoutShippingInfo){
RadioButton radioButton = new RadioButton(this);
radioButton.setText(info.name);
holder.radioShippingGroup.addView(radioButton);
//Check, if this element of radioGroup was checked in database and set checked it in radioGroup
if (info.isChecked()){
radioButton.setChecked(true);
}
}
holder.radioShippingGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
View radioButton = holder.radioShippingGroup.findViewById(checkedId);
int idx = holder.radioShippingGroup.indexOfChild(radioButton);
Log.i("TEST", "checkedId = " + Integer.toString(idx) + "; cartPosition = " + Integer.toString(position) + "; arraySize = " + Integer.toString(holder.resultCheckoutShippingInfo.size()));
databaseHelper.updateShippingInfo(
datum.get(position).getCartID,
holder.resultCheckoutShippingInfo.get(idx).getPrice() + "",
holder.resultCheckoutShippingInfo.get(idx).getTypeId() + "");
}
});
} else {
}
}
@Override
public void onFailure(@NonNull Call<CheckOutShippingInfoModel> call, @NonNull Throwable t) {
hideProgressDialog();
t.printStackTrace();
Log.e("Shipping_Response", t.getMessage() + "");
}
});
} else {
Toast.makeText(getContext(), "Please Login", Toast.LENGTH_SHORT).show();
}
}
private int grandTotal() {
int totalPrice = 0;
for (int i = 0; i < moviesList.size(); i++) {
totalPrice += moviesList.get(i).getSubtotal();
// notifyDataSetChanged();
}
return totalPrice;
}
@Override
public int getItemCount() {
return moviesList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
//RecyclerView recyclerViewShippingInfo;
//private ShippingCustomAdapter shippingCustomAdapter;
//private List<ResultCheckoutShippingInfo> resultCheckoutShippingInfo;
RadioGroup radioShippingGroup;
public MyViewHolder(View view) {
super(view);
radioShippingGroup = view.findViewById(R.id.radioShippingGroup);
//recyclerViewShippingInfo = view.findViewById(R.id.recyclerViewShippingInfo);
//recyclerViewShippingInfo.setLayoutManager(new GridLayoutManager(getContext(), 1));
//recyclerViewShippingInfo.setHasFixedSize(true);
//recyclerViewShippingInfo.setNestedScrollingEnabled(false);
}
}
}
答案 1 :(得分:1)
我建议对少数Listener
回调使用onChange
模式。并将其传递给另一个适配器。这将是更清洁的解决方案。