2013-02-25 50 views
0

你好,我使用我的自定義arrayadapter爲我的列表視圖。我使用的代碼來自vongella。適配器的代碼是下面的:listview multiple calls

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

}

的問題是,我通過5的列表所有設置爲isSelected = true的項目,並且當顯示listview時,沒有選擇任何項目,並且我在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" /> 

+0

你的佈局是什麼樣的? 'getView'被執行兩次可能是由於寬度和高度的計算沒有設置爲'fill_parent'。 – 2013-02-25 19:15:41

+0

我剛剛編輯了我的問題。這是我的佈局。 – user2065529 2013-02-25 19:19:09

+0

我不知道什麼錯誤,但是當我增添了不少項目,並檢查其中前兩個我消失後滾動,當我返回到他們在檢查箱變了! checkboxsettag()有問題 – user2065529 2013-02-25 19:33:28

回答

1

對不起各位,你在浪費你的時間看我的可怕的錯誤。錯誤在getView()裏面holder.checkBox.setSelected應該是holder.checkbox.setChecked