2 回答
TA贡献1865条经验 获得超7个赞
希望对你有帮助
protected void onPostExecute(String s) {
//Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
try {
ArrayList<String> mangLV = new ArrayList<String>();
JSONArray jsonArray = new JSONArray(s);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject object = jsonArray.getJSONObject(i);
//HoTen.getString("HoTen");
String HoTen = object.getString("HoTen");
int NamSinh = object.getInt("NamSinh");
String DiaChi = object.getString("DiaChi");
String result = String.format("HoTen: %s, NamSinh: %s, DiaChi: %s",
HoTen, NamSinh, DiaChi);
mangLV.add(result);
}
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, mangLV);
lvSinhVien.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
TA贡献1799条经验 获得超8个赞
看来你没有使用你的JSONObject jsonObject = new JSONObject(s);. 你for应该是for(int i = 0; i<=jsonArray.length();i++),从而使你的 jsonObject 过时。另外,您向适配器传递了一个空列表!你永远不会在你的 mangLV 列表中添加任何东西。
试试这个代码:
protected void onPostExecute(String s) {
//Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
try {
mangLV = new ArrayList<String>();
JSONArray jsonArray = new JSONArray(s);
for(int i = 0; i < jsonArray.length(); i++)
{
JSONObject object = jsonArray.getJSONObject(i);
String HoTen = object.getString("HoTen");
int NamSinh = object.getInt("NamSinh");
String DiaChi = object.getString("DiaChi");
String result = "HoTen " + HoTen + " | NamSinh " + NamSinh + " | DiaChi " + DiaChi;
mangLV.add(result); //After fetching the values, add the objects to your list
}
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(),android.R.layout.simple_list_item_1,mangLV);
lvSinhVien.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
这应该添加到您的 ListView 对象中,如下所示:
HoTen Nguy\u1ec5n V\u0103n A | NamSinh 1999 | DiaChi H\u00e0 N\u1ed9i
添加回答
举报