简体   繁体   中英

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);

I recomend you to implement this way:

  1. Pass a boolean array in the constructor to initialize states.
  2. Register a listener for checkbox state changes to update the states array.
  3. Implement "isChecked" to check if an item is checked at determinated position.

Using "getCount" and "isChecked" you can traverse the list from outside the adapter.

Here is the code:

public class CheckboxAdapter extends ArrayAdapter<String> {
    List<String> subcategories;
    boolean[] checked;

    public SubcategoriesAdapter(Context context, List<String> subcategories, boolean[] checked) {
        super(context, android.R.layout.simple_list_item_1);

        this.subcategories = subcategories;
        this.checked = checked;
    }

    @Override
    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));

        // Set state
        cb.setChecked(checked[position]);

        // Register listener
        cb.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                checked[position] = cb.isChecked();
            }
        });

        return convertView;
    }

    @Override
    public int getCount() {
        return subcategories.size();
    }

    @Override
    public DTOSubcategoria getItem(int position) {
        return subcategories.get(position);
    }

    public void selectAll() {
        selectAll(true);
        notifyDataSetChanged();
    }

    public void selectNone() {
        selectAll(false);
        notifyDataSetChanged();
    }

    void selectAll(boolean selected) {
        for (int i = 0; i < checked.length; i++) {
            checked[i] = selected;
        }
    }

    public boolean isChecked(int index) {
        return checked[index];
    }
}

NOTE: Once you fix your adapter, consider modifying it to use the ViewHolder pattern and so improve performance.

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);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM