从XML的Java / Android的GET阵列阵列、Java、XML、GET

2023-09-08 09:59:19 作者:蓝了天゛凉了海

我的经度和经度点在在整个我的应用程序使用的XML文件的列表。我发现我的自我重复此code经常获得积分,并认为必须有一个更好的办法?

I have a list of longitude and longitude points in an xml file that is used throughout my application. I find my self repeating this code to get points often and think there must be a better way?

    String[] mTempArray = getResources().getStringArray(R.array.stations);
    int len = mTempArray.length;
    mStationArray = new ArrayList<Station>();
    for(int i = 0; i < len; i++){
        Station s = new Station();
        String[] fields = mTempArray[i].split("[\t ]");
        s.setValuesFromArray(fields);
        Log.i("ADD STATION", ""+s);
        mStationArray.add(s);
    }

XML是格式为:

XML is in the format of:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="stations">
        <item>
            <name>Station name</name>
            <longitude>1111111</longitude>
            <latitude>11111</latitude>
            <code>1</code>
        </item>

和其他(可能的)问题是,得到的只是一个站我得个个拉一个我从阵列想要的。难道这会是相当慢?我可以在整个应用此阵是否一致? (但保持独立的意图方法)

And another (possible) problem is that to get just one station I have to get all of them and pull the one I want from the array. Is this going to be considerably slower? Can I make this array consistent throughout the app? (But keeping the separate Intent methodology)

推荐答案

我有同样想法的MilkJug,使用一个工具方法来创建站,但我想提供一个稍微不同的方法:尽可能多的建筑逻辑尽可能移动到类的构造函数。为了保持简单的例子,我在移动工具方法到类为好。

I had the same thought as MilkJug, to use a utility method to create the stations, but I want to offer a slightly different approach: Move as much of the construction logic as possible into the Station class constructor. To keep the example simple, I'm moving the utility method into the Station class as well.

这提供了一个整体的清洁设计,站类本身之外,你的code不应该要处理一个站对象,其建筑/初始化步骤尚未全面完成。

This provides an overall cleaner design, as outside of the Station class itself, your code should never have to deal with a Station object whose construction/initialization steps haven't been fully completed.

(kgiannakakis's建议使用一个数据库可能是一个更好的方式,如果你有很多车站的对象中去。)

(kgiannakakis's suggestion to use a database may be a better way to go if you have a lot of Station objects.)

public class Station {
    private static List<Station> sStationArray = null;

    /**
     * Construct a Station from a specially-encoded String. The String
     * must have all the necessary values for the Station, separated by tabs.
     */ 
    public Station(String fieldString) {
        String[] fields = fieldString.split("[\t ]");

        // For safety, setValuesFromArray() should be declared 'final'.
        // Better yet, you could just move its body into this constructor.
        setValuesFromArray(fields);

        // I'm assuming 'mName' is the name field for the Station
        Log.i("Station", this.mName);
    }

    public static Station getStationArray(Context ctx) {
        if (sStationArray == null) {

            // (Please don't use the prefix 'm' for non-member variables!)
            final String[] tempArray = 
                ctx.getResources().getStringArray(R.array.stations);
            final int len = tempArray.length;

            // Passing the length into the ArrayList constructor (if it's
            // known, or can be guessed at) can be a very simple yet
            // effective optimization. In this case the performance boost
            // will almost certainly **not** be meaningful, but it's
            // helpful to be aware of it.
            sStationArray = new ArrayList<Station>(len);    

            for (int i = 0; i < len; i++) {
                Station s = new Station(tempArray[i]);
                sStationArray.add(s);
            }
        }
        return sStationArray;
    }
}