jeudi 1 novembre 2018

How to set checkbox setChecked true from Database

I have listview with Checkbox and TextView. when I checklist that checkbox than save it. I am succesful to save it to database.

but when I am going to another activty, after that when i am going back, the checkbox didn't setChecked true just like before I did it.

In my logic, when I have database from checkbox cheked.. the checkbox should setChecked true, and the oterwise if I don't have database from it, The Checkbox should setChecked false.

this is my code:

 public class StoreFilterAdapters  extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private boolean isListView;
    private SparseBooleanArray mSelectedItemsIds;
    private DatabaseHandler databaseHandler = new DatabaseHandler();
    private List<MstStore> mItems;;
    private List<MstSelectedStore> mItemsSelected;
    private String strStateView, strValueCheck;
    boolean[] checkBoxState;
    String check;
    String idStore;

    public StoreFilterAdapters(Context context, List<MstStore> arrayList, String stateView, boolean isListView) {
        this.context        = context;
        this.mItems         = arrayList;
        this.strStateView   = stateView;
        this.isListView     = isListView;
        inflater            = LayoutInflater.from(context);
        mSelectedItemsIds   = new SparseBooleanArray();
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int i) {
        return mItems.get(i);
    }

    public void setFilter(List<MstStore> storeList) {
        mItems = new ArrayList<>();
        mItems.addAll(storeList);
        notifyDataSetChanged();
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        final MstStore ObjStore = mItems.get(i);


        if (view == null) {
            viewHolder = new ViewHolder();
            view = inflater.inflate(R.layout.item_filter_merchant, viewGroup, false);
            viewHolder.label    = view.findViewById(R.id.storeName);
            viewHolder.checkBox = view.findViewById(R.id.checkbox_filter_merchant);
            view.setTag(viewHolder);
        } else viewHolder = (ViewHolder) view.getTag();

        mItemsSelected  = databaseHandler.getAllSelectedStore();
        if (mItemsSelected.size() > 0){
            Toast.makeText(context, "ada", Toast.LENGTH_SHORT).show();
            for (int ii = 0; ii < mItemsSelected.size(); ii++){
                viewHolder.checkBox.setChecked(truemSelectedItemsIds.get(ii));
            }

        }else {
            viewHolder.checkBox.setChecked(mSelectedItemsIds.get(i));
        }
        viewHolder.label.setText(ObjStore.getStoreName());
        viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkCheckBox(i, !mSelectedItemsIds.get(i));
            }
        });

        viewHolder.label.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkCheckBox(i, !mSelectedItemsIds.get(i));
            }
        });

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkCheckBox(i, !mSelectedItemsIds.get(i));
            }
        });

        return view;
    }

    private class ViewHolder {
        private TextView label;
        private CheckBox checkBox;
    }

    public void removeSelection() {
        mSelectedItemsIds = new SparseBooleanArray();
        notifyDataSetChanged();
    }

    public void checkCheckBox(int position, boolean checked) {
        if (checked) {
            mSelectedItemsIds.put(position, true);
            check = mItems.get(position).getStoreID();

        } else {
            if (Build.VERSION.SDK_INT >= 28) {
                mSelectedItemsIds.removeAt(position);
            }else {
                mSelectedItemsIds.delete(position);
            }
        }
        notifyDataSetChanged();
    }

    /**
     * Return the selected Checkbox IDs
     **/
    public SparseBooleanArray getSelectedIds() {
        return mSelectedItemsIds;
    }

    public void getCheckedItems() {
        if (mSelectedItemsIds != null) {
            for (int i = 0; i < mItems.size(); i++) {
                if (mSelectedItemsIds.get(i)) {
                    MstStore objStore = (MstStore) getItem(i);
                    databaseHandler.addSelectedStore(new MstSelectedStore(objStore.getStoreID()));

                }
            }
        }
    }

    }

Please help me. Sorry for my bad english.




Aucun commentaire:

Enregistrer un commentaire