【问题标题】:Parse multiple values and store [ANDROID]解析多个值并存储 [ANDROID]
【发布时间】:2015-01-15 12:52:57
【问题描述】:

我想在我的 android 上解析一个 xml 格式的 webservice 响应。

我做的最“简单”的方式是这样的:

public String valor;

public static FormasDePagamento[] parseXML(String xml)
    throws ParserConfigurationException, SAXException, IOException {
    InputSource is = new InputSource(new StringReader(xml));
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();

    FormasDePagamento[] formas = new FormasDePagamento[7];
    for (int i = 0; i < 7; i++) {
        formas[i] = new FormasDePagamento();
        formas[i].valor = doc.getElementsByTagName("VALOR").item(i)
                .getTextContent();
}

但现在我需要的是获取结果中的所有内容(可以是 7 个,但也可以是 500 个结果),此外,每个存储一个列表以在活动中显示为列表。

我该如何开发它?谢谢你!

【问题讨论】:

    标签: android xml parsing


    【解决方案1】:

    您可以将获得的列表传递给ArrayAdapter 并在ListView 上显示

    ArrayAdapter<FormaDePagamento> adapter = new ArrayAdapter<FormaDePagamento>(context,android.R.layout_simple_list_item_1,parseXML(xml));
    yourListView.setAdapter(adapter);
    

    ArrayAdapter 获取您班级的toString(),因此请记住覆盖它以显示您想要的任何信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多