I have an AlerDialog
which displays a list of items with checkboxes
.I am trying to get the tag (Checkbox.getTag())
of selected checkboxes
and append them to a String
. Then i want to get this String
from the class where i have declared the AlerDialog
.
Is there a way to do that, or i should change the logic of implemetation?
Here is my CustomAdapter
:
public class DepartmentsAdapter extends BaseAdapter {
private Context mContext;
private List<DepartmentModel> departmentsList;
private LayoutInflater inflater;
public DepartmentsAdapter(Context mContext, List<DepartmentModel> departmentsList){
this.mContext = mContext;
this.departmentsList = departmentsList;
}
@Override
public int getCount() {
return departmentsList.size();
}
@Override
public Object getItem(int position) {
return departmentsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.departments_list_item, null);
DepartmentModel deptModel = (DepartmentModel) getItem(position);
CheckBox deptChBox = (CheckBox) convertView.findViewById(R.id.deptCheckBox);
TextView deptName = (TextView) convertView.findViewById(R.id.deptNameTextView);
deptChBox.setTag(deptModel.departmentCode);
deptName.setText(deptModel.departmentName);
/*deptChBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.getTag();
}
});*/
return convertView;
}
}
Here is the snippet where i am setting that list to AlertDialog
and where i am trying to get selected checkboxes
from:
final DepartmentsAdapter departmentsAdapter = new DepartmentsAdapter(mActivity, deptList);
mDialog = new AlertDialog.Builder(mActivity);
mDialog.setCancelable(true);
mDialog.setTitle(getResources().getString(R.string.departments));
mDialog.setAdapter(departmentsAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Get from selection what you want
Log.e(Constants.TAG, "Clicked CheckBOx: " + which);
final String departmentCode = deptList.get(which).departmentCode;
}
});
mDialog.show();
Thnx dudes for helping, however i managed to solve it. Here is my solution:
public class DepartmentsAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener{
private Context mContext;
private List<DepartmentModel> departmentsList;
private HashMap<String, Boolean> hashMap;
private LayoutInflater inflater;
//Constructor
public DepartmentsAdapter(Context mContext, List<DepartmentModel> departmentsList, List<String> checkedItems){
this.mContext = mContext;
this.departmentsList = departmentsList;
hashMap = new HashMap<>();
if(checkedItems != null) {
for (String item : checkedItems) {
hashMap.put(item, true);
}
}
}
@Override
public int getCount() {
return departmentsList.size();
}
@Override
public Object getItem(int position) {
return departmentsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.departments_list_item, null);
DepartmentModel deptModel = (DepartmentModel) getItem(position);
CheckBox deptChBox = (CheckBox) convertView.findViewById(R.id.deptCheckBox);
TextView deptName = (TextView) convertView.findViewById(R.id.deptNameTextView);
deptChBox.setTag(deptModel.departmentCode);
deptName.setText(deptModel.departmentName);
if(hashMap.get(deptModel.departmentCode) != null && hashMap.get(deptModel.departmentCode)){
deptChBox.setChecked(true);
}
deptChBox.setOnCheckedChangeListener(this);
return convertView;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String deptCode = (String) buttonView.getTag();
hashMap.put(deptCode, isChecked);
}
public List<String> getCheckedItems() {
ArrayList<String> checkedItems = new ArrayList<>();
for(String deptCode : hashMap.keySet()){
if(hashMap.get(deptCode)){
checkedItems.add(deptCode);
}
}
return checkedItems;
}
}
and on AlerDialog
i call it as:
final List<String> checkedItems = departmentsAdapter.getCheckedItems();
deptCodeString = Arrays.toString(checkedItems.toArray()).replace(", ", ",").replace("[", "").replace("]", "");