没有在我的代码中获取图像的资源ID

时间:2012-12-15 08:19:57

标签: android listview android-resources baseadapter

我正在尝试使用每个行中的Imageview创建一个自定义列表视图,它将通过我从数据库获取的字符串设置其图像资源。数据库只包含图像的名称,我需要从资源中加载它。我已经尝试了所有方法来获取资源的名称,但函数总是返回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());

 }

}

2 个答案:

答案 0 :(得分:0)

尝试使用它可以解决您的问题。

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

声明以下函数以从传递的可绘制字符串名称获取Drawable Resource 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;
    }
}

就是这样。

希望它对你有所帮助。

感谢。

答案 1 :(得分: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));

希望这个帮助