如何在ANDROID中的另一个Recyclerview适配器中获取第一个Recyclerview适配器项目位置

如何在ANDROID中的另一个Recyclerview适配器中获取第一个Recyclerview适配器项目位置,android,android-recyclerview,Android,Android Recyclerview,如何在另一个适配器中获取第一个适配器位置以在SQLite数据库中设置值 我想从单选按钮onClick中选择一个值,并将其保存到SQLite数据库 它工作正常,但需要单击项目的最后一个位置 所以我需要在内部适配器中的第一个适配器位置,以便在SQLite数据库中保存单选按钮的正确值 全球申报 int pos = 0; 这是我的第一个适配器代码 public class CartCustomAdapter extends RecyclerView.Adapter<CartCustomAdapt

如何在另一个适配器中获取第一个适配器位置以在SQLite数据库中设置值

我想从单选按钮onClick中选择一个值,并将其保存到SQLite数据库 它工作正常,但需要单击项目的最后一个位置

所以我需要在内部适配器中的第一个适配器位置,以便在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);



            }

        }


    }
这是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;
    }

}
这里我想更新数据库值 运费=价格 当前选项=类型ID
使用其位置

您需要创建:一个带有项的
RecyclerView.Adapter
,其中包含:照片、价格、+|-按钮、删除按钮和放射组,这些按钮具有动态单选按钮计数,使用cycle by
列表创建

公共类CartCustomAdapter扩展了RecyclerView.Adapter{ 私人名单电影名单; 公共CartCustomAdapter(列表电影列表){ this.moviesList=moviesList; } @凌驾 公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){ View itemView=LayoutInflater.from(parent.getContext()) .充气(R.layout.item\u cart\u details,parent,false); 返回新的MyViewHolder(itemView); } @凌驾 公共无效onBindViewHolder(MyViewHolder,最终整数位置){ 最终列表基准=电影列表; pos=holder.getAdapterPosition(); Log.e(“POSI1”,pos+”); if(loginModel!=null){ HashMap HashMap=新的HashMap(); hashMap.put(“option_name”,TextUtils.join(“,”,Collections.singleton(datum.get(position).getShippingOption()+”); hashMap.put(“权重”,datum.get(位置).getWeight()+”; hashMap.put(“zip”,loginModel.getResultLogin().getZip()+”); Log.e(“Parms”,hashMap+”); showProgressDialog(); 调用checkOutShippingInfoModelCall=RefughtHelper.createService(RefughtHelper.Service.class).CheckOutShippingInfoModel(hashMap); checkOutShippingInfoModelCall.enqueue(新回调(){ @凌驾 public void onResponse(@NonNull调用,@NonNull响应){ CheckOutShippingInfoModel对象=response.body(); hideProgressDialog(); if(object!=null&&object.getError()==false){ Log.e(“TAG”,“Shipping_Response:”+newgson().toJson(Response.body()); holder.resultCheckoutShippingInfo=object.getResultCheckoutShippingInfo(); //List resultCheckoutShippingInfo=new ArrayList(); //结果检查输出信息添加(“标准”); //结果CKOUTSHIPPINGINFO.add(“大成本”); for(ResultCheckoutShippingInfo:holder.ResultCheckoutShippingInfo){ RadioButton RadioButton=新的RadioButton(本); radioButton.setText(信息名称); 支架.radioShippingGroup.addView(radioButton); //选中,如果已在数据库中选中此radioGroup元素,并在radioGroup中将其设置为选中 如果(info.isChecked()){ radioButton.setChecked(真); } } holder.radioShippingGroup.setOnCheckedChangeListener(新的RadioGroup.OnCheckedChangeListener(){ @凌驾 检查更改后的公共无效(RadioGroup组,int checkedId){ 查看单选按钮=holder.radioShippingGroup.findViewById(checkedId); int idx=holder.radioShippingGroup.indexOfChild(radioButton); Log.i(“TEST”、“checkedd=“+Integer.toString(idx)+”cartPosition=“+Integer.toString(position)+”arraySize=“+Integer.toString(holder.resultCheckoutShippingInfo.size()); databaseHelper.updateShippingInfo( datum.get(位置).getCartID, holder.resultCheckoutShippingInfo.get(idx.getPrice()+“”, holder.resultCheckoutShippingInfo.get(idx.getTypeId()+); } }); }否则{ } } @凌驾 public void onFailure(@NonNull Call Call,@NonNull Throwable t){ hideProgressDialog(); t、 printStackTrace(); Log.e(“Shipping_Response”,t.getMessage()+); } }); }否则{ Toast.makeText(getContext(),“请登录”,Toast.LENGTH_SHORT.show(); } } 私有整数总计(){ int totalPrice=0; 对于(int i=0;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); }
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;
    }

}
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);



        }

    }


}