【问题标题】:List item to list view列表项到列表视图
【发布时间】:2018-12-06 13:06:45
【问题描述】:

我想将此程序转换为动态程序。从服务器获取 JSON 并返回到列表项。

public List<Item> getData() {
    return Arrays.asList(
            new Item(1, "Everyday" , "$12.00 USD"),
            new Item(2, "Small Porcelain Bowl", "$50.00 USD"),
            new Item(3, "Favourite Board", "$265.00 USD"),
    );
}

我解析了 json 但返回类型错误。

Error:(73, 8) error: incompatible types: List<String> cannot be converted to List<Item>

错误:任务 ':app:compileDebugJavaWithJavac' 执行失败。

编译失败;有关详细信息,请参阅编译器错误输出。 信息:在 13 秒内构建失败 信息:2个错误

这是我的代码:

 List<String> listItems = new ArrayList<String>();
    try {
        URL arc= new URL(
                "https://arc.000webhostapp.com/data/Cse.json");
        URLConnection tc = arc.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));

        String line;
        while ((line = in.readLine()) != null) {
            JSONArray ja = new JSONArray(line);

            for (int i = 0; i < ja.length(); i++) {
                JSONObject jo = (JSONObject) ja.get(i);
                listItems.add(jo.getString("text"));
            }
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
return listItem;

【问题讨论】:

  • 对不起,我忘记添加我的完整代码
  • 没关系,向我们展示您尝试过的方法,告诉我们哪些方法不起作用,我会投票并看看。
  • 现在请检查代码。

标签: android listview arraylist


【解决方案1】:

您已声明您的函数将返回 ListItem 对象,但您返回的是 ListString 对象。在解析从服务器获得的 JSON 时,您必须将其转换为相同的 Item 对象,就像您在代码的离线版本中所做的那样。

根据您的代码判断,您从服务器下载的 JSON 如下所示:

[
    {
        id: 1,
        text: "Everyday",
        price: "$12.00 USD"
    },
    {
        id: 2,
        text: "Small Porcelain Bowl",
        price: "$50.00 USD"
    },
    {
        id: 3,
        text: "Favourite Board",
        price: "$265.00 USD"
    },  
]

你的函数应该看起来像这样:

public List<Item> getData() {
    List<Item> listItems = new ArrayList<Item>();

    try {
        URL arc= new URL(
                "https://arc.000webhostapp.com/data/Cse.json");
        URLConnection tc = arc.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                tc.getInputStream()));

        String line;
        while ((line = in.readLine()) != null) {
            JSONArray ja = new JSONArray(line);

            for (int i = 0; i < ja.length(); i++) {
                JSONObject jo = (JSONObject) ja.get(i);
                listItems.add(new Item(jo.getInt("id"), jo.getString("text"), jo.getString("price")));
            }
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return listItems;
}

【讨论】:

    猜你喜欢
    • 2013-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多