简体   繁体   English

从任何类获取Android资源值

[英]Get Android resource value from any class

I'm just starting with Android programming and I'm still a little confused with some concepts. 我刚开始使用Android编程,但对某些概念还是有些困惑。 I'll tell you what I'm trying to do using a simplified example: 我将通过一个简化的示例告诉您我要做什么:

I'm retrieving a list of cars from a remote server (PHP/MySQL/JSON) and showing them on a ListView. 我正在从远程服务器(PHP / MySQL / JSON)检索汽车列表,并将它们显示在ListView上。

This is the server response JSON structure: 这是服务器响应JSON结构:

{
    error: null,
    data: {
        cars: [
            {name: "Lamborghini Diablo", color_id: 1},
            {name: "McLaren F1", color_id: 2},
            {name: "Ferrari F355", color_id: 1}
        ]
    }
}

Then I wrote the Car class: 然后我写了Car类:

public class Car {

    public String name;
    public int color_id;
    public String color_name;

    public Car(JSONObject data) {
        try {
            this.name = data.getString("name");
            this.color_id = data.getInt("color_id");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

And last, this is a piece of the strings.xml resource file: 最后,这是strings.xml资源文件的一部分:

<string-array name="color_names">
    <item>White</item>
    <item>Yellow</item>
    <item>Orange</item>
    <item>Red</item>
    <item>Black</item>
</string-array>

When I get the data from server, there is a for loop to create the instances of the Car class, one for each item in the JSONArray . 当我从服务器获取数据时,会有一个for循环来创建Car类的实例,一个用于JSONArray每个项目。

What I want to do is to get the color name from the <string-array> , using the property color_id as the array index, but I can't find a way to get the R.array resource from the Car class constructor. 我想做的是使用属性color_id作为数组索引从<string-array>获取颜色名称,但是我找不到从Car类构造函数中获取R.array资源的方法。

How should I do this? 我应该怎么做?

Thank You! 谢谢!

What I want to do is to get the color name from the , using the property color_id as the array index, but I can't find a way to get the R.array resource from the Car class constructor. 我想做的是使用属性color_id作为数组索引从中获取颜色名称,但是我找不到从Car类构造函数中获取R.array资源的方法。

Your class has to know "about Context" since resources are available only from Context , so here you need a little modification in your constructor: 由于资源仅可从Context获得 ,因此您的类必须了解“关于Context”,因此在这里您需要在构造函数中进行一些修改:

public Car(Context c, JSONObject data) {
   // do your stuff
}

Now, your class knows current Context so you're able to obtain data you need: 现在,您的班级知道当前的上下文,因此您可以获取所需的数据:

String name = c.getResources().getStringArray(R.id.arrayId)[<index>];

Hope it'll solve your problem. 希望它能解决您的问题。

You should provide context ot Car object or instantiate array from res in some util class with app context and provide API something like Utils.getColorNameByColorId(int color_id) . 您应该在某些util类中为应用程序上下文中的res提供Car对象的上下文或实例化数组,并提供API,例如Utils.getColorNameByColorId(int color_id) The last way is preferable because of performance - array will be instantiate only once. 由于性能,最后一种方法是可取的-数组仅实例化一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM