【问题标题】:Android Json String not filling list [duplicate]Android Json String未填充列表[重复]
【发布时间】:2012-03-09 17:31:33
【问题描述】:

嗨,我没有收到任何错误,但由于某种原因,它没有填写我的列表,json 提要中有一个新闻数组,里面有一个名为标题的标签,我想在列表视图中显示标题。但目前它只显示一个空白屏幕。这是我的代码

try {

    // Instantiate a JSON object from the request response
    JSONObject obj = new JSONObject(json);
    List<String> items = new ArrayList<String>();

    JSONArray jArray = obj.getJSONArray("news");

    for (int i = 0; i < jArray.length(); i++) {
        JSONObject oneObject = jArray.getJSONObject(i);
        items.add(oneObject.getString("title"));
    }

    setListAdapter(new ArrayAdapter<String>(this, R.layout.single_item,
            items));
    ListView list = getListView();
    list.setTextFilterEnabled(true);

    list.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(),
                    ((TextView) arg1).getText(), 1000).show();
        }

    });

} catch (Exception e) {
    // In your production code handle any errors and catch the
    // individual exceptions
    e.printStackTrace();
}

【问题讨论】:

  • 请发布您尝试解析的 JSON 样本。我们不是魔术师 ;)
  • 对不起,json {"code":200,"error":null,"data":{"news":[{"news_id":"8086","title":"Tickets年度最佳球员奖”,

标签: android json list


【解决方案1】:

要开始调试这样的事情,请弄清楚数据在哪里,又不在哪里。 Android 有一些出色的调试例程,Log.v 语句。基本上,开始把这样的代码放进去,看看你的问题出在哪里。如果您将其归结为无法正常工作的特定事物,请向我们提供该块的输入、您获得的输出以及它应该是什么。

Log.v("JSON",""+items)

【讨论】:

  • 我已经把它放进去,但在 LogCat 中没有得到任何东西 Log cat 只显示昨天的东西,即使我今天尝试使用它几次并且它没有记录任何东西
猜你喜欢
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多