嗨,请任何人帮我解决这个问题.. 我使用listview适配器显示带复选框的图像列表,允许选择多个图像格式列表..代码适用于图像列表不超过屏幕但如果图像超过屏幕(滚动)它会抛出空指针异常。以下是我的代码。
(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 :(得分:1)
正如你所说,错误是。当你滚动它时就会出局。因此,您正确地创建了您的观点,但没有正确地回收它们。
this.chkBox.add((CheckBox) vi.findViewById(R.id.check));
看起来是什么导致了您的错误。您不需要再次找到Id,因为您已经有了持有者。
holder.text.setText(text.get(position));
holder.image.setTag(data.get(position));
您可能希望在其他声明中