根据解析的XML - Android更改页面

时间:2013-10-09 19:53:13

标签: java android xml parsing

我正在开发一个包含大量信息的应用程序,这些信息需要大约700个活动来显示我需要的信息,所以我只是将信息放入XML文件并解析它,而不是进行700次活动。我想要一个Activity,它根据从XML文件中解析的信息显示信息。

<item>
   <name>Monkey</name>
   <type>flying</type>
</item>
<item>
   <name>Kangaroo</name>
   <type>Brown</type>
</item>

所以,如果我解析了Monkey,Activity会显示该Item的所有信息,在这种情况下,它会显示它是一个类型为Flying的Monkey。

示例:

解析猴子:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:layout_marginTop="17dp"
    android:text="Monkey"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="22dp"
    android:text="flying"
    android:textAppearance="?android:attr/textAppearanceMedium" />

Parsed Kangaroo:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:layout_marginTop="17dp"
    android:text="Kangaroo"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="22dp"
    android:text="Brown"
    android:textAppearance="?android:attr/textAppearanceMedium" />

1 个答案:

答案 0 :(得分:0)

我似乎无法理解你的问题,但是,是的 - 你绝对可以在Android上做到这一点。在您的示例中,您将拥有一个包含变量的对象(名称和类型)。解析xml,将xml值映射到对象成员。那么这只是TextView.setText()的问题。我建议你研究使用JSON而不是XML。