获取选定复选框的值并将其显示在下一个活动recyclerview中

时间:2019-05-09 10:35:55

标签: android checkbox android-recyclerview

我有一个recyclerview,其中包含Textview,spinner和两个复选框。选择复选框1时,将打开一个包含recyclerview项的布局。选择复选框2将打开另一个包含微调项的布局。现在,我必须收集所有值并在与recyclerview相同的另一活动中显示选定的复选框值。任何答案都是有价值的

Details2Activity.java:

 public class Details2Activity extends AppCompatActivity {

    RecyclerView rv;
    private List<TypesModel> lists;
    private APIService service;
    private MyAdapter adapter;
    private ProgressDialog pDialog;
    Button nextButton;
    String api_key,venid;
    private ArrayList<Details1PageModel> detailList=new ArrayList<>();
    private Details1PageModel details1PageModel = new Details1PageModel();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details2);

        nextButton=findViewById(R.id.details2Next_button);

        api_key="accesscode";
        venid="7";

        pDialog = new ProgressDialog(Details2Activity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

        service= Apicall.createService(APIService.class,this);

        Call<TypeList> call=service.getTypes();
        call.enqueue(new Callback<TypeList>() {
            @Override
            public void onResponse(Call<TypeList> call, Response<TypeList> response) {
                pDialog.dismiss();
                if(response.body().getStatus()){
                    lists=response.body().getTypes();
                    rv=findViewById(R.id.checkItems);
                    adapter=new MyAdapter(lists,Details2Activity.this);
                    rv.setLayoutManager(new LinearLayoutManager(Details2Activity.this,LinearLayoutManager.VERTICAL,true));
                    rv.addItemDecoration(new DividerItemDecoration(rv.getContext(), DividerItemDecoration.VERTICAL));
                    rv.setAdapter(adapter);

                }

            }

            @Override
            public void onFailure(Call<TypeList> call, Throwable t) {
                pDialog.dismiss();

            }

        });

        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                detailList.add(details1PageModel);
                service= Apicall.createService(APIService.class,Details2Activity.this);

                Call<RegCompleteModel> call = service.sendTypes(api_key, venid, detailList);
                call.enqueue(new Callback<RegCompleteModel>() {
                    @Override
                    public void onResponse(Call<RegCompleteModel> call, Response<RegCompleteModel> response) {

                        for (int i = 0; i < detailList.size(); i++) {
                            String id = response.body().getData().get(i).getId();
                            String type = response.body().getData().get(i).getType();
                            Intent j = new Intent(getApplicationContext(), Details1Activity.class);
                            j.putExtra("KeyId", id);
                            j.putExtra("keytype", type);
                            j.putExtra("keyapi", api_key);
                            j.putExtra("keyven_id", venid);
                            startActivity(j);
                        }
                    }

                    @Override
                    public void onFailure(Call<RegCompleteModel> call, Throwable t) {
                        Toast.makeText(getApplicationContext(), "Something went Wrong!!", Toast.LENGTH_SHORT).show();

                    }

                });

            }
        });

    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>  {
        private List<TypesModel> arrayList;
        private Context context;
        private String[] spinValues = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
        private List<SportsModel> sportsModelList;
        private SportAdapter adapter;
        private List<String> typical1;


        public MyAdapter(List<TypesModel> arrayList, Context context) {
            this.arrayList = arrayList;
            this.context = context;
        }


        @NonNull
        @Override
        public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.check_items, parent, false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(@NonNull final MyAdapter.ViewHolder holder, int i) {

            final int pos = i;
            final TypesModel fm = arrayList.get(i);
            holder.types.setText(fm.getType());
            holder.checkTypes.setChecked(arrayList.get(i).isSelected());

            holder.checkTypes.setTag(arrayList.get(i));
            holder.checkTypes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    CheckBox cb = (CheckBox) v;
                    TypesModel types1 = (TypesModel) cb.getTag();

                    types1.setSelected(cb.isChecked());
                    arrayList.get(pos).setSelected(cb.isChecked());

             //       details1PageModel.setType(Integer.parseInt(fm.getId()));

                    //       Toast.makeText(v.getContext(), "Clicked " + cb.getText() + cb.isChecked(), Toast.LENGTH_LONG).show();

                }
            });

            holder.checkSplit.setTag(arrayList.get(i));
            holder.checkSplit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    CheckBox cb1 = (CheckBox) v;
                    TypesModel types2 = (TypesModel) cb1.getTag();

                    types2.setSelected(cb1.isChecked());
                    arrayList.get(pos).setSelected(cb1.isChecked());
                }
            });
            holder.checkTypes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if(isChecked){
                        details1PageModel.setType(Integer.parseInt(fm.getId()));
                        holder.relative.setVisibility(View.VISIBLE);
                        holder.rv=holder.itemView.findViewById(R.id.list_sports);
                        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
                        layoutManager.setFlexDirection(FlexDirection.ROW);
                        holder.rv.setLayoutManager(layoutManager);
                        holder.recycleView();
                    }else{
                        details1PageModel.setType(0);
                        holder.relative.setVisibility(View.GONE);
                    }
                }
            });

            holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    details1PageModel.setNumber(Integer.parseInt(spinValues[position]));
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    details1PageModel.setNumber(0);
                }
            });
            holder.spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String split1= holder.spinner1.getItemAtPosition(position).toString();



                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
            holder.spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String split2= holder.spinner2.getItemAtPosition(position).toString();



                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });    

        }    
        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public int getItemCount() {
            return arrayList.size();
        }    

        public class ViewHolder extends RecyclerView.ViewHolder {

            TextView types;
            CheckBox checkTypes, checkSplit;
            Spinner spinner,spinner1,spinner2;
            RelativeLayout relative;
            private RecyclerView rv;
            private LinearLayout ll;
            String tyid,tyid1;

            public ViewHolder(final View itemView) {
                super(itemView);

                types = itemView.findViewById(R.id.list_type);
                checkTypes = itemView.findViewById(R.id.list_checkbox);
                checkSplit = itemView.findViewById(R.id.check_splitable);
                spinner = itemView.findViewById(R.id.list_spinner);
                relative = itemView.findViewById(R.id.relative5);
                ll = itemView.findViewById(R.id.linearLayout15);
                spinner1=itemView.findViewById(R.id.split_spin1);
                spinner2=itemView.findViewById(R.id.split_spin2);

                spinner.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinValues));


                checkSplit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked) {
                            ll.setVisibility(View.VISIBLE);
                            details1PageModel.setSplitable(1);
                            typical1=new ArrayList<>();
                            for(int i=0;i<arrayList.size();i++){
                                tyid=arrayList.get(i).getId();
                                typical1.add(arrayList.get(i).getType());
                            }
                            List<String> types1=new ArrayList<>();
                            for(int i=0;i<arrayList.size();i++){
                                tyid1=arrayList.get(i).getId();
                                types1.add(arrayList.get(i).getType());
                            }

                            spinner1.setAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_spinner_dropdown_item,typical1));
                            spinner2.setAdapter(new ArrayAdapter<String>(context,android.R.layout.simple_spinner_dropdown_item,types1));    
                        } else {
                            details1PageModel.setSplitable(0);
                            ll.setVisibility(View.GONE);
                        }
                    }
                });

            }  

            private void recycleView() {

                service= Apicall.createService(APIService.class,context);

                Call<SportsList> call = service.getSportName();
                call.enqueue(new Callback<SportsList>() {
                    @Override
                    public void onResponse(Call<SportsList> call, Response<SportsList> response) {

                        sportsModelList = response.body().getSports();
                        rv = itemView.findViewById(R.id.list_sports);
                        adapter = new SportAdapter(sportsModelList, context);
                        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
                        layoutManager.setFlexDirection(FlexDirection.ROW);
                        rv.setLayoutManager(layoutManager);
                        rv.setAdapter(adapter);
                    }
                    @Override
                    public void onFailure(Call<SportsList> call, Throwable t) {
                        Toast.makeText(context, "Something went Wrong!!", Toast.LENGTH_SHORT).show();

                    }
                });
            }
        }
    } 
}

Details1PageModel.java:

public class Details1PageModel {

    private int type;
    private int number,splitable;
    private List<Integer> forId;
    private List<String> split;


    public void setType(int type) {
        this.type = type;
    }

    public void setSplitable(int splitable) {
        this.splitable = splitable;
    }

    public void setSplit(List<String> split) {
        this.split = split;
    }

    public List<String> getSplit() {
        return split;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void setForId(List<Integer> forId) {
        this.forId = forId;
    }

    public List<Integer> getForId() {
        return forId;
    }

    public int getType() {
        return type;
    }

    public int getSplitable() {
        return splitable;
    }

    public int getNumber() {
        return number;
    }
}

SportAdapter.java:

 public class SportAdapter extends RecyclerView.Adapter<SportAdapter.ViewHolder> {
    private List<SportsModel> sports;
    private Context context;
    private SparseBooleanArray selectedItem=new SparseBooleanArray();
    private Details1PageModel details1PageModel=new Details1PageModel();
    private List<Integer> sport_id;

    public SportAdapter(List<SportsModel> sports, Context context) {
        this.sports = sports;
        this.context = context;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.check_types, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {

        final SportsModel tm = sports.get(position);
        holder.type.setText(tm.getSportName());

        holder.type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectedItem.get(position, false)) {
                    selectedItem.delete(position);
                    v.setBackgroundColor(Color.WHITE);
                    v.setSelected(false);

                }
                else {
                    selectedItem.put(position, true);
                    v.setBackgroundColor(context.getResources().getColor(android.R.color.holo_blue_light) );
                    v.setSelected(true);
                     sport_id = new ArrayList<Integer>();
                     int[] ids = new int[]{Integer.valueOf(tm.getId())};

                         for (int j : ids) {
                             sport_id.addAll(Collections.singleton(j));
                         }

                       Log.d("List: ",sport_id.toString());
                    details1PageModel.setForId(sport_id);
                   }
                }
        });
}
    @Override
    public int getItemCount() {
        return sports.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView type;

        public ViewHolder(View itemView) {
            super(itemView);

            type = itemView.findViewById(R.id.types);

        }
    }

}

我必须发布此值type [] = {“ type”:1,“ number”:2,“ splitable”:1,“ for”:[3,2],“ split”:[{“ id “:1},{” id“:2}]}

0 个答案:

没有答案