图像未在自定义列表中设置

时间:2013-11-22 12:58:37

标签: android android-imageview

我从image中选择gallery并在sqlite DB中保存其路径,所有内容都正常,但image未在ImageView中设置。 MyAdapter.java类是:

public class MyAdapter extends CursorAdapter {

protected Activity activity = null;


public MyAdapter(Activity activity, Cursor cursor) {
    super(activity, cursor, false);     
    this.activity = activity;
}

@Override
public void bindView(View row, Context context, Cursor cursor) {

    MyHolder holder = (MyrHolder) row.getTag();
    holder.populateFrom(cursor);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup root) {

    LayoutInflater inflater = activity.getLayoutInflater();

    View row = inflater.inflate(R.layout.row, root, false);
    MyHolder holder = new MyHolder(activity, row);
    row.setTag(holder);

    return (row);
}

}

MyHolder.java课程是:

public class MyHolder {

protected Activity activity = null;
protected TextView name, phone, dateOfJoining;
protected ImageView imageView = null;

    public MyHolder(Activity activity, View row) {

    this.activity = activity;
    name = (TextView) row.findViewById(R.id.name);
    phone = (TextView) row.findViewById(R.id.phone);
    dateOfJoining = (TextView) row.findViewById(R.id.joinDate);
    imageView = (ImageView) row.findViewById(R.id.imageView);
}

    public void populateFrom(Cursor cursor) {

        name.setText(cursor.getString(0));
        phone.setText("Cell : " + cursor.getString(1));
        dateOfJoining.setText(cursor.getString(2));         
        imageView.setImageBitmap(getImageBitmap(cursor));


}

protected Bitmap getImageBitmap(Cursor cursor) {

    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeFile(cursor.getString(3));
    } catch (Exception e) {

        bitmap =BitmapFactory.decodeResource(activity.getResources(), R.drawable.person);
    }
    Log.e("IAMGE", "Path is = " + cursor.getString(3));
    return bitmap;
}}

但图片未在行imageView的{​​{1}}中设置。 请告诉我如何设置这个问题..

3 个答案:

答案 0 :(得分:1)

可能还有更多选项,但我认为你应该在第

行给出正确的列索引
bitmap = BitmapFactory.decodeFile(cursor.getString(3));

这可能对你有所帮助......

答案 1 :(得分:0)

在提供的代码中,您在newView中创建了MemberHolder的实例,并且您已经为MyHolder粘贴了代码,所以我假设MemberHolder和MyHolder是相同的,这是一些错字......

您正在创建会员持有人的实例,理想情况下应该是MyHolder .....

MemberHolder holder = new MemberHolder(activity, row);

populateFrom持有者的方法是填充视图的方法,但似乎没有从任何可能的地方调用,因为View没有被填充,而ImageView也是空的。

答案 2 :(得分:0)

试试这个......

除去

holder.populateFrom(cursor);

来自

bindView(View row, Context context, Cursor cursor)

方法并将其保存在newView(...)方法中,如

@Override
public View newView(Context context, Cursor cursor, ViewGroup root) {
     LayoutInflater inflater = activity.getLayoutInflater();
     View row = inflater.inflate(R.layout.row, root, false);
     MyHolder holder = new MyHolder(activity, row);
     row.setTag(holder);
     holder.populateFrom(cursor);
     return (row);
}