lundi 19 mars 2018

how to save values of checkboxes in arraylist

i have made one recycler view. In recycler view i have putted checkboxes and text and select all button on top. On Selectall button all checkboxes become true. and on second time click all checkboxes become false. now i just want to store values of checkboxes in arraylist on button click on On Unselect button Arraylist become clear . and also if i have selected only 2 or 3 or anything it should be store in arraylist.

   public class SearchFragment extends BaseFragment {
private static final String TAG = null;

ArrayList<String> arrayList = new ArrayList<>();
public TextView tv2,tv3,tv4,tv5,tv6,tv7;
CheckBox abc,abc1,a,b,c;
public RecyclerView rvList;
Context context;
public static SearchFragment newInstance () {
    return new SearchFragment();
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_search, container, false);
    tv2=(TextView) view.findViewById(R.id.tv2);
    tv3=(TextView) view.findViewById(R.id.tv3);
    tv4=(TextView) view.findViewById(R.id.tv4);
    tv5=(TextView) view.findViewById(R.id.tv5);
    tv6=(TextView) view.findViewById(R.id.tv6);
    tv7=(TextView) view.findViewById(R.id.tv7);

    abc=(CheckBox) view.findViewById(R.id.abc);
    a=(CheckBox) view.findViewById(R.id.a);
    b=(CheckBox) view.findViewById(R.id.b);
    c=(CheckBox) view.findViewById(R.id.c);

    rvList=(RecyclerView)view.findViewById(R.id.rvList);
    arrayList = new ArrayList<>();
    for (int i = 0; i < 10; i++){
        arrayList.add("Produk " + i);
    }
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());

    rvList.setLayoutManager(layoutManager);

    MyAdapter mAdapter = new MyAdapter(getContext(),arrayList);

    // 4. set adapter
    rvList.setAdapter(mAdapter);
    // 5. set item animator to DefaultAnimator
    rvList.setItemAnimator(new DefaultItemAnimator());


    return view;
}

@Override
public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ButterKnife.bind(this, view);


    tv2.setOnClickListener(new View.OnClickListener() {
@Override
     public void onClick(View v) {

     MyAdapter mAdapter = new MyAdapter(getContext(),arrayList);

     rvList.setAdapter(mAdapter);

     if(AppContants.CheckFlag){
     AppContants.CheckFlag=false;

      }
      else{
    AppContants.CheckFlag=true;

            }

    mAdapter.notifyDataSetChanged();
        }

   @OnClick({R.id.txtFilter})
    public void onClick (View view) {
      switch (view.getId()) {
        case R.id.txtFilter:
            startIntent(FilterActivity.class);
            getActivity().overridePendingTransition(R.anim.left_in, R.anim.plain);
            break;
    }
    }


   public class  MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private HashMap<Integer, Boolean> isChecked = new HashMap<>();
    private Context mContext;
    private LayoutInflater inflater;
    CheckBox cbProduk;

    ArrayList<String> arrayList = new ArrayList<>();

    public MyAdapter(Context context, List<String> arrayList){
        this.mContext = context;
        this.arrayList = (ArrayList<String>) arrayList;
        inflater = LayoutInflater.from(context);
    }

    public MyAdapter(ArrayList<String> arrayList) {


    }

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = inflater.inflate(R.layout.list_item_produt, parent, false);

        MyViewHolder holder = new MyViewHolder(rootView);
        cbProduk=(CheckBox)rootView.findViewById(R.id.cbProduk);
        return holder;
    }


    @Override
    public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
        holder.cbProduk.setText(arrayList.get(position));

        if (AppContants.CheckFlag) {
            cbProduk.setChecked(true);


        } else  {
                cbProduk.setChecked(false);
        }



    }


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

    public void NotifyDatasetchanged() {

    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        CheckBox cbProduk;

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

            cbProduk = (CheckBox) itemView.findViewById(R.id.cbProduk);

            cbProduk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        // KONDISI PADA SAAT CEKLIS
                        Toast.makeText(mContext, "checklist", Toast.LENGTH_SHORT).show();
                    } else {
                        // KONDISI PADA SAAT CEKLIS DIHILANGKAN
                        Toast.makeText(mContext, "unchecklist", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}

}




Aucun commentaire:

Enregistrer un commentaire