こんにちは、リストビューにカスタム 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;
}
}
カテゴリ行の詳細は次のとおりです。
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 が 2 回実行されることを意味しますすべてのアイテム。
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" />