检查复选框从arrayadapter获得 [英] Check Checkbox getting from arrayadapter

查看:126
本文介绍了检查复选框从arrayadapter获得的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有标题的复选框的列表,我想有哪一个会在默认情况下进行检查控制研究。所以我想,以获得正确的观点,并检查它,但由于某种原因,这是行不通的。任何想法,为什么?

form_checkbox_item.xml

 <?XML版本=1.0编码=UTF-8&GT?;

 的android:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:比重=中心
机器人:方向=横向>
<复选框
    机器人:ID =@ + ID / checkBox1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =40dp
    机器人:文字颜色=@彩色/ background_red
    机器人:填充=12dp
    /><的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =10dp
    机器人:文字=TextView的
    机器人:文字颜色=@彩色/ background_red
    机器人:TEXTSIZE =18sp/>

CheckboxAdapter.java

 包com.rgis.datacollection.ui.adapters;进口android.app.Activity;
进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.CheckBox;
进口android.widget.TextView;进口com.rgis.datacollection.R;进口的java.util.List;公共类CheckboxAdapter扩展ArrayAdapter
{
    上下文语境;
    清单<串GT; checkboxItems;
公共CheckboxAdapter(上下文的背景下,列表与LT;弦乐>资源)
{
    超(背景下,R.layout.form_checkbox_item,资源);    this.context =背景;
    this.checkboxItems =资源;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{    LayoutInflater充气=((活动)上下文).getLayoutInflater();
    convertView = inflater.inflate(R.layout.form_checkbox_item,父母,假);
    TextView中的TextView =(TextView中)convertView.findViewById(R.id.textView1);
    复选框CB =(复选框)convertView.findViewById(R.id.checkBox1);    textView.setText(checkboxItems.get(位置));
    返回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 =新CheckboxAdapter(背景下,StringList的);
    listView.setAdapter(checkboxAdapter);    复选框CB =(复选框)checkboxAdapter.getView(0,空,ListView控件).findViewById(R.id.checkBox1);
    cb.setChecked(真);


解决方案

的一种方式是有布尔在你的适配器,它重新presents每一个复选框的状态列表。你需要初始化的假,然后用setCheckbox方法更改特定复选框的状态

 公共类CheckboxAdapter扩展ArrayAdapter
{
   上下文语境;
   清单<布尔> checkboxState;
   清单<串GT; checkboxItems;
   公共CheckboxAdapter(上下文的背景下,列表与LT;弦乐>资源)
   {
      超(背景下,R.layout.form_checkbox_item,资源);      this.context =背景;
      this.checkboxItems =资源;
      this.checkboxState =新的ArrayList<布尔>(Collections.nCopies(resource.size(),真));
   }   公共查看getView(INT位置,查看convertView,父母的ViewGroup){      LayoutInflater充气=((活动)上下文).getLayoutInflater();
convertView = inflater.inflate(R.layout.form_checkbox_item,父母,假);
      TextView中的TextView =(TextView中)convertView.findViewById(R.id.textView1);
      复选框CB =(复选框)convertView.findViewById(R.id.checkBox1);      textView.setText(checkboxItems.get(位置));
      cb.setChecked(checkboxState.get(位置));
      返回convertView;
   }

这方法让你控制哪些复选框被活化

 无效setChecked(布尔状态,INT位置)
  {
     checkboxState.set(位置,状态);
  }

I have list of title-checkbox, and i want to have controll 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"?>

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" />

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

解决方案

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

这篇关于检查复选框从arrayadapter获得的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆