Bruse Bruse - 2 months ago 19
Android Question

Check CheckBox getting from arrayadapter

I have list of title-

CheckBox
, and i want to have control which one will be checked on default. So I'm trying to get the right view and check it, but for some reason it doesn't work. any idea why?

form_checkbox_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:textColor="@color/background_red"
android:padding="12dp"
/>

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="TextView"
android:textColor="@color/background_red"
android:textSize="18sp" />

</LinearLayout>


CheckboxAdapter.java

package com.rgis.datacollection.ui.adapters;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.rgis.datacollection.R;

import java.util.List;

public class CheckboxAdapter extends ArrayAdapter
{
Context context;
List<String> checkboxItems;
public CheckboxAdapter(Context context, List<String> resource)
{
super(context, R.layout.form_checkbox_item ,resource);

this.context = context;
this.checkboxItems = resource;
}

public View getView(int position, View convertView, ViewGroup parent)
{

LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.form_checkbox_item, parent, false);
TextView textView = (TextView) convertView.findViewById(R.id.textView1);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);

textView.setText(checkboxItems.get(position));
return convertView;
}
}


part of formCheckBox.java

formItem = (LinearLayout) linflater.inflate(R.layout.checkbox_layout, null);
listView = (ListView) formItem.findViewById(R.id.checkboxList);
stringList = dbUtils.getServiceConfigForFixedOptions(attribute.getListValues());
CheckboxAdapter checkboxAdapter = new CheckboxAdapter(context, stringList);
listView.setAdapter(checkboxAdapter);

CheckBox cb = (CheckBox) checkboxAdapter.getView(0, null, listView).findViewById(R.id.checkBox1);
cb.setChecked(true);

Answer

One way is having a list of booleans in your adapter which represents the state of every checkbox. You need to init that to false and then with a setCheckbox method change the state of a specific checkbox

public class CheckboxAdapter extends ArrayAdapter
{
   Context context;
   List<Boolean> checkboxState;
   List<String> checkboxItems;
   public CheckboxAdapter(Context context, List<String> resource)
   {
      super(context, R.layout.form_checkbox_item ,resource);

      this.context = context;
      this.checkboxItems = resource;
      this.checkboxState = new ArrayList<Boolean>(Collections.nCopies(resource.size(), true));
   }

   public View getView(int position, View convertView, ViewGroup parent){

      LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.form_checkbox_item, parent, false);
      TextView textView = (TextView) convertView.findViewById(R.id.textView1);
      CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1);

      textView.setText(checkboxItems.get(position));
      cb.setChecked(checkboxState.get(position));
      return convertView;
   }

This method let you control which checkboxes are actived

  void setChecked(boolean state, int position)
  {
     checkboxState.set(position, state);
  }
Comments