2010-10-26 64 views
2

嗨請任何人幫我出來這.. 我使用的ListView適配器顯示圖像列表與複選框,允許選擇多個圖像形式列表..代碼適用於圖像列表不超過屏幕,但如果圖像超過屏幕(用於滾動),它會拋出空指針異常..以下是我的代碼。listview與imagelist不適合多個選擇在custome適配器

(apterMultiChoice.java)

public class PhotoAdapterMultiChoice extends BaseAdapter 
    { 
public ViewHolder holder; 
private Activity activity; 
private ArrayList<String> data; 
private ArrayList<String> text; 
private ArrayList<CheckBox> chkBox; 
private static LayoutInflater inflater=null; 
public ImageLoader imageLoader; 

    public PhotoAdapterMultiChoice(Activity a, ArrayList<String> title,ArrayList<String>   URL,ArrayList<CheckBox> chkBoxPass) 
{ 
    activity = (Activity) a; 
    data=URL; 
    text=title; 
    this.chkBox = chkBoxPass; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    imageLoader=new ImageLoader(activity.getApplicationContext()); 
} 
public int getCount() 
{ 
    return data.size(); 
} 
public boolean getSelect(int position) 
{ 
    return this.chkBox.get(position).isChecked(); 
} 
public int getCountSelect() 
{ 
    return chkBox.size(); 
} 
public Object getItem(int position) 
{ 
    return this.chkBox.get(position).isChecked(); 
} 
public long getItemId(int position) 
{ 
    return position; 
} 
public static class ViewHolder 
{ 
    public TextView text; 
    public ImageView image; 
    public CheckBox checkBox; 
} 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    System.out.println(this.chkBox.size() +" ---- "+data.size()); 
    if(this.chkBox.size() < data.size()) 
    { 
     View vi=convertView; 
     if(convertView==null) 
     { 
      System.out.println("convertView is null"); 
      vi = inflater.inflate(R.layout.photoitemlistmultichoice, null); 
      holder=new ViewHolder(); 
      holder.text=(TextView)vi.findViewById(R.id.text); 
      holder.image=(ImageView)vi.findViewById(R.id.image); 
      holder.checkBox=(CheckBox) vi.findViewById(R.id.check); 
      //this.chkBox.add((CheckBox) vi.findViewById(R.id.check)); 
      vi.setTag(holder); 
     } 
     else 
     { 
      System.out.println("convertView is Not null"); 
      this.chkBox.add((CheckBox) vi.findViewById(R.id.check)); 
      holder=(ViewHolder)vi.getTag(); 
     } 

     System.out.println("Position : "+position); 
     holder.text.setText(text.get(position)); 
     holder.image.setTag(data.get(position)); 
     //holder.checkBox.setTag(chkBox.get(position)); 
     //this.chkBox.set(position, chkBox.get(position)); 
     //this.chkBox.add(holder.checkBox); 
     imageLoader.DisplayImage(data.get(position), activity, holder.image,"Photo"); 
     return vi; 
    } 
    return null; 
} 

} 

我調用適配器

PhotoAdapter adapter = new PhotoAdapter(this, imageNameList, thumbURLList); 
listPhoto.setAdapter(adapter); //listPhoto is my listView 

(photolist.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<TextView 
    android:id="@+id/lblAlbumName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
<ListView 
    android:id="@+id/listPhoto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"/> 
</LinearLayout> 

(photoitemlist.xml)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:id="@+id/image" 
     android:layout_width="50dip" 
     android:layout_height="50dip" android:src="@drawable/default_photo_icon" android:scaleType="centerCrop"/> 
    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="left|center_vertical" 
     android:textSize="20dip" 
     android:layout_marginLeft="10dip" 
     /> 
</LinearLayout> 

請幫助..

感謝...

+0

我的意思是這行給出了嗎? – 2012-01-30 18:33:42

回答

1

的錯誤是,如你所說。當你滾動它的時候。因此,您正確創建您的視圖,但不正確回收它們。

this.chkBox.add((CheckBox) vi.findViewById(R.id.check)); 

看起來像是什麼導致了你的錯誤。你不需要再次找到身份證,因爲你已經擁有了身份證。

holder.text.setText(text.get(position)); 
     holder.image.setTag(data.get(position)); 

你可能想在你的else語句