自定义列表视图imageview错误

时间:2012-01-10 10:43:39

标签: android android-layout

您好我正在使用texview,imageview创建自定义列表视图,我正在从sqlite数据库访问数据,我在其中存储每个图像的名称,并在我尝试使用holder.mimage.setImageResource访问它时将图像放在assests文件夹中(list.get(位置).getImage()); 它给出了错误我如何访问它任何建议将非常有帮助

public class InteractiveArrayAdapter extends ArrayAdapter<PosHolder> {
String tag = "Events";
private final List<PosHolder> list;
private final Activity context;
int li,jh;



public InteractiveArrayAdapter(Activity context, List<PosHolder> list) {
    super(context, R.layout.row, list);
    this.context = context;
    this.list = list;
}

 static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox,checkbox1;
    protected RadioGroup mgroup;
    protected RadioButton mbutton;
    protected ImageView mimage;
}




public View getView( final int position, View convertView, ViewGroup parent) {
    //Log.d(tag," 3");

     View view =null;
    if (convertView == null) {
        //System.out.println("ok");
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.row, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.textView1);

        viewHolder.mimage = (ImageView) view.findViewById(R.id.imageView1);




           view.setTag(viewHolder);
           viewHolder.mimage.setTag(list.get(position));


    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).mimage.setTag(list.get(position));


    }
     ViewHolder holder = (ViewHolder) view.getTag();
    view.getTag()).mgroup.getTag();
     holder.text.setText(list.get(position).getName());
     holder.mimage.setImageResource(list.get(position).getImage());//This line is showing the error

    return view;

}
public class PosHolder {
private String name;
private String smallimage;


public PosHolder(String name, String smallimage) {
    // TODO Auto-generated constructor stub

    this.name=name;
    this.smallimage=smallimage;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getImage() {
    return smallimage;
}
public void setImage(String phone) {
    this.smallimage = phone;
}}

2 个答案:

答案 0 :(得分:0)

你要将图像名称传递给imageView而不是IMAGE。您正在从PosHolder对象列表中查找图像的imageName instand。你的PosHolder对象为你提供字符串而不是图像

public String getImage() 
{     
    return smallimage; 
}

这导致了异常。

答案 1 :(得分:0)

错误是因为您没有实例化“ViewHolder”类的字段。
在您将图像资源设置为它时它们仍为空。