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.