1

このようなxmlで単純な配列を作成する方法を知っています

<resources>
    <string-array name="countries_array">
        <item>Afghanistan</item>
        <item>Albania</item>
        <item>Algeria</item>
        <item>American Samoa</item>
        <item>Andorra</item>
        <item>Angola</item>
        <item>Anguilla</item>
        <item>Antarctica</item>
    </string-array>
</resources>

そして、コードでこのように初期化します

ArrayAdapter<String> adapter = 
        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries_array);
textView.setAdapter(adapter);

しかし、単なる文字列ではなく、より多くのアイテムを格納したいので、単純な配列ではなく配列リストを格納する方法が必要です。

以下のように

<resources>
<Share>
    <name>Apple</name>
    <currentRate>5.69</currentRate>
    <changeToday>-0.11</changeToday>
    <changeTodayPercent>-0.02</changeTodayPercent>
    <timeUpdated>2012,12,06,18,00,00</timeUpdated>
<Share>
<Share>
    <name>Microsoft</name>
    <currentRate>5.88</currentRate>
    <changeToday>0.19</changeToday>
    <changeTodayPercent>+0.09</changeTodayPercent>
    <timeUpdated>2012,12,06,18,00,00</timeUpdated>
<share>
...
</resources>

setMethods を介して設定する必要がある次の変数を持つ Share.java というクラスが既にあります

public class ShareHolding {

private String name;
private double currentRate;
private double changeToday;
private double changeTodayPercent;
private GregorianCalendar timeUpdated;
}

それらをarraylistに追加します

ArrayList<Share> allShares = new ArrayList<Share>();

allShares.getShare(0).setName("I want to access my xml file here and add the first shares name here");
4

1 に答える 1

1

私の知る限り、リソース ファイルを使用してデータ型を一致させることはできません。リソースは 一方向のデータ型のみをサポートします。例: 文字列の配列、整数の配列など。

うまくいく最善の解決策は、共有xmlを独自のファイルにし、解析を共有オブジェクトにすることです。Android 開発者には、組み込みの xml パーサーの例がいくつかあります。

于 2012-12-06T20:03:11.900 に答える