通过Android中的名称访问绘图 [英] Accesing to a drawable through its name in Android

查看:51
本文介绍了通过Android中的名称访问绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个处理SQLite数据库的应用程序.我只是从中获取一些信息,并将其显示在CardViews列表中.每个CardView基本上都会显示一个人的姓名,年龄和照片.

I'm developing an app thant handles a SQLite database. I just get some information from it and show it in a list of CardViews. Each CardView basically displays the name, the age and a picture of a person.

一切正常,但是现在我已经实现了数据库,对此我有些疑问.

Everything is working fine, but now that I have implemented the database, I have some doubts about it.

我只需要通过文件名访问绘图文件.到目前为止,我一直在使用R.drawable id进行手动操作,因此可以检查它是否在工作.就是这样:

I just need to access to the drawables through the name of the files. So far I've been working manually with the R.drawable id, so I could check if it was working. Just like this:

人员p1 =新人员(姓名,年龄,R.drawable.person1);

Person p1 = new Person(name, age, R.drawable.person1);

然后我将那个Person添加到Persons列表中,然后将此列表作为参数传递给Adapter.

And then I just add that Person to a List of Persons, to then pass this List as a parameter to the Adapter.

基本上,人名和图像名完全相同.因此,既然我从数据库中获得了所有人员(列表中),我想从文件名中获取可绘制对象,但是现在我仅是如何通过图片的R.drawable.id来获取它..

Basicaly, the name of the person and the name of the image is exactly the same. So, now that I get all of the persons from the database (in a list), I'd like to get the drawable from the file name, but I only now how to get it through the R.drawable.id of the picture.

我想将R.drawable属性从 int 更改为 String ,这样我就可以使用想要的方式.

I'd like to change the R.drawable attribute from an int to a String, so I can access the way I want.

我的问题是,是否有做类似 getDrawable("name_of_the_file.png"); 之类的东西?还是有什么方法可以通过文件名获取R.drawable.id?

My question is, is there anyway to do something like getDrawable("name_of_the_file.png"); or anything similar? Or is there any way to get the R.drawable.id through the name of the file?

谢谢.

推荐答案

无论如何都要做类似getDrawable("name_of_the_file.png")的事情;或类似的东西?

is there anyway to do something like getDrawable("name_of_the_file.png"); or anything similar?

否.

或者有什么方法可以通过文件名来获取R.drawable.id?

Or is there any way to get the R.drawable.id through the name of the file?

)和资源名称(例如"example" ).

Call getIdentifier() on a Resources object to retrieve the numeric R value given the package, resource type (e.g., "drawable"), and resource name (e.g., "example").

这篇关于通过Android中的名称访问绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆