gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:14:26 26 4
gpt4 key购买 nike

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

公共(public)类 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);
}
}
}

这是我查询数据库并将数组传递给自定义适配器类的主要 Activity 。 TextView 根据位置正确设置。唯一的问题是从资源文件夹中获取图像的 int id。公共(public)类 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());

}

最佳答案

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

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;
}
}

就是这样。

希望对你有帮助。

谢谢。

关于android - 在我的代码中没有获取图像的资源 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13890567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com