2010-10-26 67 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语句