[英]Get value from CheckBox in custom ListView
我已经实现了一个HashMap,其中键是一个字符串,值是一个整数,我想知道如何获得单击的单选按钮的值,获取值,然后我可以用它们做一些操作,例如:
Cb1 2已检查
Cb2 9已检查
Cb3 19 UnChecked
输出应为11(2 + 9)
所以我想做一些已检查的并用该值更新TextView。
从现在开始我有这个:
这是HashMap
HashMap<String, Integer> hmAs = new LinkedHashMap<String, Integer>();
这就是我创建Dialog的方式
Dialog mDialog;
mDialog=new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.custom_dialog_as);
ListView lv = (ListView) mDialog.findViewById(R.id.listView);
Button bt = (Button) mDialog.findViewById(R.id.btAcceptAs);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
//Here I need the sum of all of the values
}
});
ArrayAdapter adapter = new HashMapArrayAdapter(this, android.R.layout.simple_list_item_single_choice, new ArrayList(hmAs.entrySet()));
lv.setAdapter(adapter);
mDialog.show();
这是适配器
class HashMapArrayAdapter extends ArrayAdapter {
Context mContext;
private static class ViewHolder {
TextView tV1;
CheckBox cb;
}
public HashMapArrayAdapter(Context context, int textViewResourceId, List<Map.Entry<String, Integer>> objects) {
super(context, textViewResourceId, objects);
this.mContext = context;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_item_dialog, parent, false);
viewHolder = new ViewHolder();
viewHolder.tV1 = (TextView) convertView.findViewById(R.id.tvDataAs);
viewHolder.cb = (Checkbox) convertView.findViewById(R.id.checkbox);
convertView.setTag(viewHolder);
} else
viewHolder = (ViewHolder) convertView.getTag();
Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>) this.getItem(position);
viewHolder.tV1.setText(entry.getKey());
viewHolder.rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(mContext, String.valueOf(entry.getValue()), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
有了这个Toast
我得到了map
的确切值,但问题是如何从对话框中,当我按下“接受”时我有Button
我可以获得ListView
的所有CheckBox
的总和
好吧,我最终做了这样的事情:
我创建了一个名为Sessio
的类,它包含一个int
(sessio的值)和一个boolean
来检查是否选中了复选框。 好吧,我必须保存在SharedPreferences
,我一直在这里阅读SO,你会建议不要在SharedPreferences
上保存HashMap
,因为它需要大量的内存,所以...这个想法是一样的,我必须保存所有的hashmap以及是否点击了哪一个,我认为计算CheckBox
的东西......
按“接受”我可以得到ListView的所有CheckBox的总和
在HashMapArrayAdapter
创建一个返回已检查条目值的方法,并在Accept
Button上调用它点击:
1.声明一个HashMap,其位置为key, entry.getValue()
为存储已检查CheckBox值的键:
Map<Integer, String> checkedCheckBoxValues = new HashMap<Integer, String>();
2.现在创建一个在HashMapArrayAdapter
类中返回HashMap<Integer, String>
的HashMapArrayAdapter
:
public HashMap<Integer, String> getCheckedCheckBoxs(){
return checkedCheckBoxValues;
}
3.将onCheckedChanged
键和值添加到checkedCheckBoxValues
:
checkedCheckBoxValues.put(position,String.valueOf(entry.getValue()));
4.使用adapter
在Accept
按钮上调用getCheckedCheckBoxs
方法:
@Override
public void onClick(View v) {
mDialog.dismiss();
HashMap<Integer, String> checkedValues=adapter.getCheckedCheckBoxs();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.