自定义列表适配器中的动态图像设置

时间:2015-08-31 14:56:44

标签: android database imageview adapter drawable

我正在尝试从数据库中获取动态图像并将其放入适配器

我的数据库中有列(图像)具有每个图像的路径

喜欢@ drawable / pic8

我尝试用它运行应用程序,但它崩溃了

 package com.example.tarekhy;

 import java.io.File;
 import android.app.Activity;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.drawable.Drawable;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.ImageView;
 import android.widget.TextView;

 public class CustomListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final String[] imgid;

public CustomListAdapter(Activity context, String[] itemname, String[] imgid) {
    super(context, R.layout.mylist, itemname);
    // TODO Auto-generated constructor stub

    this.context=context;
    this.itemname=itemname;
    this.imgid=imgid;
}

public View getView(int position,View view,ViewGroup parent) {
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.mylist, null,true);

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
    int imageResource = context.getResources().getIdentifier(imgid[position], null, context.getPackageName());
    Drawable res = context.getResources().getDrawable(imageResource);
    txtTitle.setText(itemname[position]);
    imageView.setImageDrawable(res);
    extratxt.setText("Description "+itemname[position]);
    return rowView;

};
}

2 个答案:

答案 0 :(得分:0)

因为您没有从ViewHolder类创建新对象..您在此视图上创建持有者和访问权

    public static class ViewHolder {
           public TextView txtTitle ;
        public ImageView imageView;
        public TextView  extratxt;


        }

     View vi = convertView;
                final ViewHolder holder;
    public View getView(int position,View view,ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
                try {
                    if (convertView == null) {
                        vi = inflater.inflate(R.layout.mylist, null,true);
                        holder = new ViewHolder();
                        vi.setTag(holder);
                    } else {
                        holder = (ViewHolder) vi.getTag();
                    }

                        holder.txtTitle =  (TextView) vi.findViewById(R.id.item);
                        holder.imageView=  (ImageView) vi.findViewById(R.id.item);
                        holder.extratxt=  (TextView) vi.findViewById(R.id.item);
                        int imageResource = context.getResources().getIdentifier(imgid[position], null, context.getPackageName());
                        Drawable res = context.getResources().getDrawable(imageResource);
                        holder.txtTitle.setText(itemname[position]);
                        holder.imageView.setImageDrawable(res);
                        holder.extratxt.setText("Description "+itemname[position]);
                        return rowView;


    }
    };

答案 1 :(得分:0)

R.layout.myList怎么样......你应该使用项目的资源ID ..像R.layout.mylist_item这样的东西 顺便说一句,如果你重新定位视图,它会更好..而不是每次都给它们充气..使用Viewholder视图更好