检查从 arrayadapter 获取的 CheckBox

Check CheckBox getting from arrayadapter

我有标题列表-CheckBox,我想控制默认检查哪一个。所以我试图获得正确的视图并检查它,但由于某种原因它不起作用。知道为什么吗?

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

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

一种方法是在你的适配器中有一个布尔值列表,它代表每个复选框的状态。您需要将其初始化为 false,然后使用 setCheckbox 方法更改特定复选框的状态

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

此方法可让您控制激活哪些复选框

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

我建议你这样实现:

  1. 在构造函数中传递一个布尔数组来初始化状态。
  2. 注册复选框状态更改的侦听器以更新状态数组。
  3. 实施"isChecked"检查项目是否在确定的位置被检查。

使用 "getCount" 和 "isChecked" 您可以从适配器外部遍历列表。

代码如下:

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

注意:修复适配器后,请考虑修改它以使用 ViewHolder 模式,从而提高性能。