我有标题列表 - 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);
答案 0 :(得分:3)
我建议你这样实现:
使用“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模式,从而提高性能。
答案 1 :(得分:1)
一种方法是在适配器中包含一个布尔值列表,表示每个复选框的状态。您需要将其初始化为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);
}