jeudi 9 mai 2019

Get Selected checkbox values and display it in next activity recyclerview

I have a recyclerview which contains Textview,spinner and two checkboxes. While selecting checkbox 1 opens a layout with recyclerview items. Selecting checkbox 2 opens another layout with spinner items. Now I have to collect all the values and display the selected checkbox values in another activity same as recyclerview. Any answer will be valuable

public class Details2Activity extends AppCompatActivity {

RecyclerView rv;
private List<TypesModel> lists;
private APIService service;
private MyAdapter adapter;
private ProgressDialog pDialog;
Button nextButton;

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

    nextButton=findViewById(R.id.details2Next_button);

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

    Retrofit retrofit=new Retrofit.Builder()
            .baseUrl(URLs.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    service=retrofit.create(APIService.class);

    Call<TypeList> call=service.getTypes();
    call.enqueue(new Callback<TypeList>() {
        @Override
        public void onResponse(Call<TypeList> call, Response<TypeList> response) {
            pDialog.dismiss();
            if(response.isSuccessful()){
                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) {

            Intent i=new Intent(Details2Activity.this,Details1Activity.class);
            startActivity(i);

        }
    });

}

}


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


    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) {
        TypesModel fm = arrayList.get(i);
        holder.types.setText(fm.getTypes());
        holder.checkTypes.setChecked(arrayList.get(i).isSelected());

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

                Integer pos = (Integer) holder.checkTypes.getTag();
                Toast.makeText(context, arrayList.get(pos).getTypes() + " clicked!", Toast.LENGTH_SHORT).show();

                if (arrayList.get(pos).isSelected()) {
                    arrayList.get(pos).setSelected(false);
                } else {
                    arrayList.get(pos).setSelected(true);
                }
            }
        });


    }

    @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;
        RelativeLayout relative;
        private RecyclerView rv;
        private SportAdapter adapter;
        private List<SportsModel> sportsModelList;
        private LinearLayout ll;

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

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

            checkTypes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        relative.setVisibility(View.VISIBLE);
                        rv=itemView.findViewById(R.id.list_sports);
                        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(context);
                        layoutManager.setFlexDirection(FlexDirection.ROW);
                        rv.setLayoutManager(layoutManager);
                        recycleView();
                    }else{
                        relative.setVisibility(View.GONE);
                    }
                }
            });

            checkSplit.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        ll.setVisibility(View.VISIBLE);
                    }
                    else{
                        ll.setVisibility(View.GONE);
                    }
                }
            });

        }

        private void recycleView() {

            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl(URLs.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            APIService service=retrofit.create(APIService.class);

            Call<SportsList> call=service.getSportName();
            call.enqueue(new Callback<SportsList>() {
                @Override
                public void onResponse(Call<SportsList> call, Response<SportsList> response) {
                    sportsModelList=response.body().getSports();
                    adapter=new SportAdapter(sportsModelList,context);
                    rv.setAdapter(adapter);
                }

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

                }

            });


        }
    }

    }

public class TypesModel {

@SerializedName("id")
private int id;

@SerializedName("type")
private String types;

private boolean isSelected;

public TypesModel(int id,String types){
    this.id=id;
    this.types=types;
}

public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean selected) {
    isSelected = selected;
}

public int getId() {
    return id;
}

public String getTypes() {
    return types;
}

}

I have to post this values type[]={"type":1,"number":2,"splitable":1,"for":[3,2],"split":[{"id":1},{"id":2}]}




Aucun commentaire:

Enregistrer un commentaire