【发布时间】:2016-03-03 18:19:01
【问题描述】:
我知道已经回答了几个类似的问题,但是因为我对 Android 开发非常陌生,所以我无法自己解决这个问题。这个问题是不言自明的。我正在通过 HTTP 请求从数据库中获取数据,并希望将这些数据调整为列表视图。我将 Volley 用于我的 HTTP 请求,下面是我的 onResponse -方法。
其他一切都完美无缺,我只是还没有找到一种方法将这些数据调整到列表视图中。
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
// If we are getting success from server
try {
JSONObject jObject = new JSONObject(response);
int count = jObject.getInt("count");
if(count == 0) {
noEventsTextView.setText(jObject.getString("msg").toString());
noEventsTextView.setGravity(Gravity.CENTER);
noEventsImageView.setVisibility(View.VISIBLE);
} else {
JSONArray childArray = jObject.getJSONArray("lectures");
for(int i = 0; i < childArray.length(); i++) {
JSONObject finalObject = childArray.getJSONObject(i);
// TODO Adapt data to listView
}
}
} catch(JSONException e) {
e.printStackTrace();
}
}
}
这是我从服务器返回的 JSON 示例:
{
count: 2,
msg: "",
lectures: [
{
id: "1",
starting_at: "2015-11-30 13:00:00",
ending_at: "2015-11-30 15:00:00",
user_id: "1",
course: "Course #1",
user_name: "John Doe"
},
{
id: "2",
starting_at: "2015-11-30 13:00:00",
ending_at: "2015-11-30 15:00:00",
user_id: "1",
course: "Course #2",
user_name: "John Doe"
}
]
}
【问题讨论】:
-
使用键参数值将所有值存储在添加到基本适配器的列表中。
标签: android json listview android-listview