0

您好,我正在将我的自定义 arrayadapter 用于我的列表视图。我正在使用vongella的代码。适配器的代码如下:

public class CategoriesAdapter extends ArrayAdapter<CategoriesRowDetails>{
private final Activity context;
private final List<CategoriesRowDetails> categories;


public CategoriesAdapter(Activity context,
        List<CategoriesRowDetails> categories) {
    super(context, R.layout.categories_row, R.id.textViewCategory, categories);
    this.context = context;
    this.categories = categories;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if(rowView == null){
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.categories_row, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.imageView = (ImageView) rowView.findViewById(R.id.imageViewCategory);
        viewHolder.textView = (TextView) rowView.findViewById(R.id.textViewCategory);
        viewHolder.checkBox = (CheckBox) rowView.findViewById(R.id.checkBoxCategory);
        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                CategoriesRowDetails element = (CategoriesRowDetails) viewHolder.checkBox.getTag();
                element.setSelected(buttonView.isChecked());
            }
        });
        rowView.setTag(viewHolder);
        viewHolder.checkBox.setTag(categories.get(position));
    }else{
        ((ViewHolder) rowView.getTag()).checkBox.setTag(categories.get(position));
    }

    ViewHolder holder = (ViewHolder) rowView.getTag();

    holder.textView.setText(categories.get(position).getName());

    int resResult = context.getResources().getIdentifier(categories.get(position).getResName(), "drawable",
            ".my_tour_guide");

    if(resResult == 0){

        holder.imageView.setImageResource(R.drawable.stop);
    }else{
        holder.imageView.setImageResource(resResult);
    }
    Log.i("CategoriesAdapter", String.valueOf(categories.get(position).isSelected()) + "position = " + String.valueOf(position));
    holder.checkBox.setSelected(categories.get(position).isSelected());
    return rowView;
}

    private static class ViewHolder{
    public ImageView imageView;
    public TextView textView;
    public CheckBox checkBox;
}

}

categoriesRowDetails 如下:

public class CategoriesRowDetails {

private String name;
private String resName;
private boolean selected;

public CategoriesRowDetails(String name, boolean isSelected){
    setName(name);
    setResName(name);
    setSelected(isSelected);
}

private void setName(String name) {
    this.name = name;
}

public String getName(){
    return name;
}

private synchronized void setResName(String name) {

     name = name.toLowerCase();
     name = name.replace(" ", "_");
     this.resName = name;
}

public synchronized String getResName(){
    return resName;
}

public boolean isSelected(){
    return selected;
}

public void setSelected(boolean selected){
    this.selected = selected;
}

}

问题是我传递了一个全部设置为 isSelected = true 的 5 个项目的列表,当显示列表视图时,没有选择任何项目,而且我在 logcat 中看到以下消息,这意味着 getview 执行了两次每个项目。

02-25 20:43:51.160: I/CategoriesAdapter(14185): trueposition = 0
02-25 20:43:51.190: I/CategoriesAdapter(14185): trueposition = 1
02-25 20:43:51.200: I/CategoriesAdapter(14185): trueposition = 2
02-25 20:43:51.210: I/CategoriesAdapter(14185): trueposition = 3
02-25 20:43:51.220: I/CategoriesAdapter(14185): trueposition = 4
02-25 20:43:51.660: I/CategoriesAdapter(14185): trueposition = 0
02-25 20:43:51.660: I/CategoriesAdapter(14185): trueposition = 1
02-25 20:43:51.670: I/CategoriesAdapter(14185): trueposition = 2
02-25 20:43:51.670: I/CategoriesAdapter(14185): trueposition = 3
02-25 20:43:51.670: I/CategoriesAdapter(14185): trueposition = 4

编辑添加布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageViewCategory"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:paddingLeft="10dp" />

<TextView
    android:id="@+id/textViewCategory"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/checkBoxCategory"
    android:layout_toRightOf="@+id/imageViewCategory"
    android:lines="1"
    android:paddingLeft="5dp"
    android:scrollHorizontally="true"
    android:textSize="24sp" />

<CheckBox
    android:id="@+id/checkBoxCategory"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_centerVertical="true"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" />

4

1 回答 1

1

我为你们所有人感到抱歉,你们浪费了时间看我的可怕错误。错误getView()在行内holder.checkBox.setSelected应该是holder.checkbox.setChecked

于 2013-02-25T20:03:32.537 回答