我有一个AlerDialog
,其中显示项目列表 checkboxes
。我正在尝试获取所选tag (Checkbox.getTag())
的{{1}}和将它们附加到checkboxes
。然后我想从我声明String
的类中获取此String
。
有没有办法做到这一点,或者我应该改变实施的逻辑?
这是我的AlerDialog
:
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
答案 0 :(得分:1)
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("]", "");
答案 1 :(得分:0)
您可以在适配器类
中尝试这样的操作 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;
}
}
现在您可以在对话框类中调用它,没有任何问题。 希望它有所帮助...
答案 2 :(得分:0)