从绑定在 AlertDialog 上的自定义适配器获取多个复选框的标记
Getting tag of Multiple Checkbox from Custom Adapter binded on AlerDialog
我有一个 AlerDialog
,它显示 项目列表 和 checkboxes
。我正在尝试获取所选 tag (Checkbox.getTag())
=13=] 并将它们附加到 String
。然后我想从我声明 AlerDialog
的 class 得到这个 String
。
有没有办法做到这一点,或者我应该改变实现的逻辑?
这是我的 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;
}
}
这是我将该列表设置为 AlertDialog
以及我试图从中选择 checkboxes
的片段:
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();
您可以在您的适配器中尝试类似的操作 class
private CheckBox mSelectedRB;
private int selected = -1;
private String title = "";
然后在你的 getView 方法中
title = this.items.get(position);
// deptName.setText(title);
deptChBox.setTag(title);
deptChBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(position != selected && mSelectedRB != null){
mSelectedRB.setChecked(false);
}
selected = position;
mSelectedRB = (CheckBox)view;
selectedUrl = (String)view.getTag();
//notifyDataSetInvalidated();
}
});
if(selected != position){
deptChBox.setChecked(false);
}else{
deptChBox.setChecked(true);
if(deptChBox != null && deptChBox != mSelectedRB){
mSelectedRB = deptChBox;
}
}
现在您可以在对话框中毫无问题地调用它 class。
希望对你有帮助...
检查这个 http://www.ulduzsoft.com/2012/07/android-dialog-to-choose-a-directory-or-file-based-on-alertdialog/ 可能有用...
Thnx 伙计们帮忙,但我设法解决了它。这是我的解决方案:
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;
}
}
在 AlerDialog
上我称之为:
final List<String> checkedItems = departmentsAdapter.getCheckedItems();
deptCodeString = Arrays.toString(checkedItems.toArray()).replace(", ", ",").replace("[", "").replace("]", "");
我有一个 AlerDialog
,它显示 项目列表 和 checkboxes
。我正在尝试获取所选 tag (Checkbox.getTag())
=13=] 并将它们附加到 String
。然后我想从我声明 AlerDialog
的 class 得到这个 String
。
有没有办法做到这一点,或者我应该改变实现的逻辑?
这是我的 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;
}
}
这是我将该列表设置为 AlertDialog
以及我试图从中选择 checkboxes
的片段:
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();
您可以在您的适配器中尝试类似的操作 class
private CheckBox mSelectedRB;
private int selected = -1;
private String title = "";
然后在你的 getView 方法中
title = this.items.get(position);
// deptName.setText(title);
deptChBox.setTag(title);
deptChBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
if(position != selected && mSelectedRB != null){
mSelectedRB.setChecked(false);
}
selected = position;
mSelectedRB = (CheckBox)view;
selectedUrl = (String)view.getTag();
//notifyDataSetInvalidated();
}
});
if(selected != position){
deptChBox.setChecked(false);
}else{
deptChBox.setChecked(true);
if(deptChBox != null && deptChBox != mSelectedRB){
mSelectedRB = deptChBox;
}
}
现在您可以在对话框中毫无问题地调用它 class。 希望对你有帮助...
检查这个 http://www.ulduzsoft.com/2012/07/android-dialog-to-choose-a-directory-or-file-based-on-alertdialog/ 可能有用...
Thnx 伙计们帮忙,但我设法解决了它。这是我的解决方案:
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;
}
}
在 AlerDialog
上我称之为:
final List<String> checkedItems = departmentsAdapter.getCheckedItems();
deptCodeString = Arrays.toString(checkedItems.toArray()).replace(", ", ",").replace("[", "").replace("]", "");