How to get the key value pair in multiple check checkbox

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to get the key value pair in multiple check checkbox

Postby Debojyoti » Sun Aug 19, 2012 7:23 am

I am new to android, can anybody please help to get the key and corresponding value(check/not-check) in a checkbox.

There can be more than one checked item in checkbox. I have to get all the items for which user has "checked" in the checkbox. I am using arrayAdapter for displaying the checkbox. Following is code of the arrayAdapter which handles "setOnCheckedChangeListener"

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private final List<Model> list;
  2. private final Activity context;
  3. private static final String TAG = "InteractiveArrayAdapter";
  4.  
  5. public InteractiveArrayAdapter(Activity context, List<Model> list) {
  6.     super(context, R.layout.rowbuttonlayout, list);
  7.     this.context = context;
  8.     this.list = list;
  9. }
  10.  
  11. static class ViewHolder {
  12.     protected TextView text;
  13.     protected CheckBox checkbox;
  14. }
  15.  
  16. @Override
  17. public View getView(int position, View convertView, ViewGroup parent) {
  18.     View view = null;
  19.     if (convertView == null) {
  20.         LayoutInflater inflator = context.getLayoutInflater();
  21.         view = inflator.inflate(R.layout.rowbuttonlayout, null);
  22.         final ViewHolder viewHolder = new ViewHolder();
  23.         viewHolder.text = (TextView) view.findViewById(R.id.label);
  24.         viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
  25.         viewHolder.checkbox
  26.                 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  27.  
  28.                     @Override
  29.                     public void onCheckedChanged(CompoundButton buttonView,
  30.                             boolean isChecked) {
  31.                         Model element = (Model) viewHolder.checkbox.getTag();
  32.                         //String temp = viewHolder.checkbox.getText().toString();
  33.                         //Log.d(TAG, temp);
  34.                         //String temp = buttonView.getText().toString();
  35.                         element.setSelected(buttonView.isChecked());
  36.  
  37.                     }
  38.                 });
  39.         view.setTag(viewHolder);
  40.         viewHolder.checkbox.setTag(list.get(position));
  41.     } else {
  42.  
  43.         view = convertView;
  44.         ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
  45.     }
  46.  
  47.     ViewHolder holder = (ViewHolder) view.getTag();
  48.     holder.text.setText(list.get(position).getName());
  49.     holder.checkbox.setChecked(list.get(position).isSelected());
  50.     return view;
  51. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Debojyoti
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Aug 19, 2012 7:17 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests