1

Explanation: I have a one dialog. In which, i have listview where all the data is set into the listview and show into the dialog. I able to select multiple values from the listview and then click on apply button i got the selected values data.

Problem

The problem is when i open the dialog again my previous selection is not selected in my code.

What i want?

When i again open the dialog my previous selection is remain selected in the listview item. How can i do this?

item_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#333044">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ll_btn_actions">
        <ListView
            android:id="@+id/item_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_btn_actions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/cancel"
            android:background="@android:color/transparent"
            android:textColor="@color/white"
            android:textSize="@dimen/meet_details_fs"
            android:gravity="center"
            android:textAllCaps="false"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/btn_submit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/apply"
            android:background="@android:color/transparent"
            android:textColor="@color/white"
            android:textSize="@dimen/meet_details_fs"
            android:gravity="center"
            android:textAllCaps="false"
            android:layout_weight="1"/>
    </LinearLayout>
</RelativeLayout>

When i press the button the dialog will be open here is the code of open the dialog.

public void setitemDialog(final Activity activity, final List<Brand> compactStoreItemList) {
        itemDialog = new Dialog(activity);
        itemDialog.setContentView(R.layout.item_dialog);
        itemDialog.setCancelable(true);
        itemDialog.setTitle("Select Brands");
        itemDialog.setCanceledOnTouchOutside(true);

        Button btnSubmit = (Button) itemDialog.findViewById(R.id.btn_submit);
        Button btnCancel=(Button)itemDialog.findViewById(R.id.btn_cancel);

        final ListView itemList = (ListView) itemDialog.findViewById(R.id.item_list);
        final BrandAdapter adapter = new BrandAdapter(activity, compactStoreItemList, prefManager);
        itemList.setAdapter(adapter);

        itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.selectItem(position);
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemDialog.isShowing()) {
                    itemDialog.dismiss();
                }
            }
        });
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                brandList = new ArrayList<>();
                String brandNames = "";
                for (Brand a : adapter.getBox()) {
                    if (a.isChecked) {
                        Log.e(TAG, "BRAND IDS=>" + a.getId());
                        brandList.add(a);
//                        tempArr.add(a);
                        brandNames += a.getBrandName() + ",";
                    }
                }
//                for(Brand b:tempArr){
//
//                    brandNames += b.getBrandName() + ",";
//                }
                spiBrand.setText(brandNames);
                if (itemDialog.isShowing()) {
                    itemDialog.dismiss();
                }
            }
        });
        adapter.notifyDataSetChanged();
        itemDialog.show();
    }

Below is the adapter which is called by the dialog and set the values into the listview.

public class BrandAdapter extends BaseAdapter {

    public static final String TAG=BrandAdapter.class.getSimpleName();

    public Activity activity;
    private List<Brand> brandList;
    private LayoutInflater inflater;
    PreferenceSetting prefManager;

    public BrandAdapter(Activity activity, List<Brand> brandList, PreferenceSetting preferenceSetting){
        this.activity=activity;
        this.brandList=brandList;
        prefManager=PreferenceSetting.getInstance(activity);
        inflater=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Object getItem(int position) {
        return brandList.get(position);
    }

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

    public void selectItem(int position){
        brandList.get(position).isChecked = !brandList.get(position).isChecked;
        notifyDataSetChanged();
    }
    public ArrayList<Brand> getBox() {
        ArrayList<Brand> box = new ArrayList<>();
        for (Brand p : brandList) {
            if (p.isChecked)
                box.add(p);
        }
        return box;
    }
    private class Holder{
        TextView brandName;
        CheckBox checkBox;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        Holder holder;
        if(convertView==null){
            holder=new Holder();
            convertView=inflater.inflate(R.layout.ac_problems_list,parent,false);

            holder.brandName=(TextView)convertView.findViewById(R.id.txt_cooling);
            holder.checkBox=(CheckBox)convertView.findViewById(R.id.cb_cooling);

            convertView.setTag(holder);
        }
        else{
            holder=(Holder)convertView.getTag();
        }

        final Brand brand=brandList.get(position);
        holder.brandName.setText(brand.getBrandName());
//        for(Brand brand1:MeetingActivity.tempArr){
//            if(brand.getId()==brand1.getId()){
//                holder.checkBox.setChecked(brand1.isChecked);
//                Log.e(TAG, "IS CHECKED=>" + brand.isChecked);
//            }
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectItem(position);
            }
        });
        return convertView;
    }
}

Brand.java

public class Brand {

    public static final String BRAND_ID="id";
    public static final String BRAND_NAME="brand_name";
    public static final String USER_ID="user_id";
    public static final String DELETE_STATUS="delete_status";
    public static final String DISABLE_STATUS="disable_status";
    public static final String CREATED_ON="created_on";

    private int id;
    private String brandName;
    private String userId;
    private String deleteStatus;
    private String disableStatus;
    private String createdOn;
    public boolean isChecked = false;

    public Brand(){}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getDeleteStatus() {
        return deleteStatus;
    }

    public void setDeleteStatus(String deleteStatus) {
        this.deleteStatus = deleteStatus;
    }

    public String getDisableStatus() {
        return disableStatus;
    }

    public void setDisableStatus(String disableStatus) {
        this.disableStatus = disableStatus;
    }

    public String getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(String createdOn) {
        this.createdOn = createdOn;
    }
}

How can i do this?? Please help me out.

Milan Gajera
  • 962
  • 2
  • 14
  • 41

2 Answers2

0

In your getView method, you should check for isChecked on your brand object and implement the logic there. (showing the correct design state according to its isChecked value)

I've done something similar to this, here :

@Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        Contract currentContract = mDataset.get(position);
    if (app.isContractSelected()) {
                if (currentContract.getContractNr() == app.getSelectedContract().getContractNr()) {
                    contractViewHolder.selector.setVisibility(View.VISIBLE);
                    contractViewHolder.listItem.setBackgroundColor(ContextCompat.getColor(mContext, R.color.veryLightGrey));
                } else {
                    contractViewHolder.selector.setVisibility(View.INVISIBLE);
                    TypedValue outValue = new TypedValue();
                    mContext.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
                    contractViewHolder.listItem.setBackgroundResource(outValue.resourceId);
                }
            }}
Greg
  • 689
  • 2
  • 8
  • 23
  • When i open dialog again my adapter is refresh this is the main problem. – Milan Gajera Nov 24 '16 at 10:29
  • Yes, but getView() from your adapter is called each time you reopen the dialog. If you update isChecked on your Brand object each time you select/deselect, and read its value in getView you should be able to achieve what you want – Greg Nov 24 '16 at 10:31
  • But how can i achieved i don't know? – Milan Gajera Nov 24 '16 at 10:32
  • isChecked is only true when i click on the checkbox otherwise it's false. So, When my dialog is open again my adapter is refresh. so the all the ischecked values for all the position is false by default. – Milan Gajera Nov 24 '16 at 10:33
  • You need to always be working with the same list. I suspect you are probably passing a new list to setItemDialog? Try this : debug your app and put a breakpoint just after setitemDialog. Inspect there the content of your List compactStoreItemList. Do you have some checked set to true? – Greg Nov 24 '16 at 10:41
  • all the checked values is false. – Milan Gajera Nov 24 '16 at 10:49
  • Try to understand why. it is the first step of your problem :) When you click on an item, you should set isChecked to true. and make sure you are using / saving the Brand list somewhere, and always use the same one. In the Application class for example : http://stackoverflow.com/questions/4208886/using-the-android-application-class-to-persist-data – Greg Nov 24 '16 at 10:52
0

You could save the state of the checkboxes for example in SharedPreferences when your dialog is closing (before naturally) and then when your dialog is restarted again, you will need to read those values from the SharedPreferences and then set the values in the initialization of your dialog. You can put boolean values in the sharedpreferences and then read those out again when the dialog is being created.

zaifrun
  • 931
  • 9
  • 21