3

我正在尝试在每一行中使用 Imageviews 制作一个自定义列表视图,该视图将通过我从数据库中获取的字符串设置其图像资源。数据库只包含图像的名称,我需要从资源中加载它。我已经尝试了所有按名称获取资源的方法,但该函数始终返回 0。无论我做什么,我都无法根据名称获取我的 id。这是我正在使用的自定义适配器....

公共类 OwnAddptor 扩展 BaseAdapter {

private LayoutInflater mInflater;
String[] names;
String[] detailarray;
String[] adapimage;
int size;
Context mycont;


public OwnAddptor(Context context,String[] poi_names,String[] details,String[] imageadap) {

        mInflater = LayoutInflater.from(context);
        this.names = poi_names;
        this.detailarray = details;
        this.adapimage = imageadap;
        this.mycont = context;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return names.length;
}

@Override
public Object getItem(int puhj) {
    // TODO Auto-generated method stub
    return puhj;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
     ViewHolder holder;

        if (vi == null) {
            vi = mInflater.inflate(R.layout.blockview, null);
            holder = new ViewHolder();
            holder.headline = (TextView) vi.findViewById(R.id.head_line);
            holder.detail = (TextView) vi.findViewById(R.id.detail);
            holder.image = (ImageView) vi.findViewById(R.id.image);
            vi.setTag(holder);
        } 
        else {  
            holder = (ViewHolder) vi.getTag();  
        }  
        holder.headline.setText(names[position]);
        holder.detail.setText(detailarray[position]);
       // Drawable imgdraw = mycont.getResources().getDrawable(adapimage[position]);
        holder.image.setImageDrawable(getAndroidDrawable(adapimage[position], mycont));
        System.out.println(adapimage[position]);
        return vi;

    }

    static class ViewHolder {
        TextView headline;
        TextView detail;
        ImageView  image;
    }
    public int getImageId(Context context, String name) {
        System.out.println("current name"+name);
        return context.getResources().getIdentifier("drawable/"+name, null, context.getPackageName());



    }
    static public Drawable getAndroidDrawable(String pDrawableName,Context cont){

        int resourceId=Resources.getSystem().getIdentifier(pDrawableName, "drawable", cont.getPackageName());
        System.out.println("resource ids****"+resourceId);
        if(resourceId==0){
            return null;
        } else {
            return Resources.getSystem().getDrawable(resourceId);
        }
    }
}

这是我查询数据库并将数组传递给自定义适配器类的主要活动。文本视图根据位置正确设置。唯一的问题是从资源文件夹中获取图像的 int id。公共类 MainActivity 扩展 Activity {

DataBaseHelper baseHelper ;
SQLiteDatabase database;
OwnAddptor adapcustom;
 TextView  detail;
 ListView  view;
 TextView headline;
 String[] name;
 String[] images;
 String[] desc;
 int[] imgids;   
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    List<RowItem> rowItems;

      view = (ListView) findViewById(R.id.mylist);
     baseHelper = new DataBaseHelper(this);
        try
        {
         baseHelper.createDatabase(); 
        }
        catch (IOException ioe)
        {
           throw new Error("Unable to create database");
        }
        try
        {
          baseHelper.openDataBase();

        }
        catch(SQLException sqle)
        {
           throw sqle;
        }
      database = baseHelper.getReadableDatabase();
      Cursor cursor= database.rawQuery("Select * from tblArpoi_local", null);
      int i = 0;

          if (cursor.getCount()>0) {
              cursor.moveToFirst();
              name = new String[cursor.getCount()];
              desc = new String[cursor.getCount()];
              images= new String[cursor.getCount()];
              imgids = new int[cursor.getCount()];
      while (cursor.moveToNext()) {
        images[i]= "poi_"+cursor.getString(cursor.getColumnIndex("arPoiImageLink"));
        name[i] = cursor.getString(cursor.getColumnIndex("arPoiName"));
        desc[i] = cursor.getString(cursor.getColumnIndex("arPoiDescription"));

                i++;
    }



      System.out.println("****"+images.length);
      database.close();
      cursor.close();
      adapcustom = new OwnAddptor(this,name,desc,images);
      view.setAdapter(adapcustom);




          }
}
 public static int getImageId(Context context, String name) {
        System.out.println("current name"+name);
        return context.getResources().getIdentifier("drawable/"+name, null, context.getPackageName());

 }

}

4

2 回答 2

0

我用这个

context.getResources().getIdentifier(drawableResourceName, "drawable", context.getPackageName());

它就像一个沙姆一样工作。

你可以试着打电话

holder.image.setImageDrawable(context.getResources().getIdentifier(drawableResourceName, "drawable", context.getPackageName()));

代替

holder.image.setImageDrawable(getAndroidDrawable(adapimage[position], mycont));

希望这有帮助

于 2012-12-15T17:35:55.220 回答
0

试试这个,它会解决你的问题。

ImageView my_image = (ImageView) findViewById(R.id.my_imageView);
my_image.setBackgroundResource(getResId("ic_launcher"));

声明以下函数以从传递的可绘制字符串名称中获取可绘制资源 ID。

public static int getResId(String drawableName) {    
    try {
        Class res = R.drawable.class;
        Field field = res.getField(drawableName);
        int drawableId = field.getInt(null);
        System.out.println("resource ids****"+drawableId);
        return drawableId;
    } catch (Exception e) {
        Log.e("MyTag", "Failure to get drawable id.", e);
        return 0;
    }
}

而已。

希望它可以帮助你。

谢谢。

于 2012-12-15T09:16:16.577 回答