3

我有一个 android/java 任务,我想将 JSON 值放入字符串数组中以显示在 ListView 中,但我不确定从哪里开始?谢谢。

private String[] values;
...
// this is what is returned from the web server (Debug view)
// jObj = {"success":1,"0":"Mike","1":"message 1","2":"Fred","3":"message 2","4":"John","5":"message 3"};

try {
   if (jObj.getInt("success") == 1) {
      .
      // what i'm trying to do here is iterate thru JObj and assign values to the 
      // values array to populate the ArrayAdapter so that the ListView displays this:
      //
      // Mike: Message 1
      // Fred: Message 2
      // John: Message 3
      //
      .
      this.setListAdapter(new ArrayAdapter<String>(
         this, android.R.layout.simple_list_item_1, android.R.id.text1, values));
      ListView listView = getListView();
   }
}
catch (JSONException e) {
   Log.e(TAG, e.toString());
}
4

3 回答 3

1

使用ArrayList而不是Array添加从 Json Obejct 检索到的值,然后将 ListView 的 ArrayList 设置为数据源。将您的代码更改为:

 ArrayList<String> array_list_values = new ArrayList<String>();
try {
       if (jObj.getInt("success") == 1) {

        array_list_values.add(jObj.getString("0"));
        array_list_values.add(jObj.getString("1"));
        array_list_values.add(jObj.getString("2"));
        array_list_values.add(jObj.getString("3"));
        array_list_values.add(jObj.getString("4"));
        array_list_values.add(jObj.getString("5"));
        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, array_list_values));
                ListView listView = getListView();
            }
     }
catch (JSONException e) {
      Log.e(TAG, e.toString());
  }

编辑:如果消息的数量并不总是相同,它可能是 1 到更大的数量,那么您可以将 JsonObject 迭代为:

 ArrayList<String> array_list_values = new ArrayList<String>();
try {
       if (jObj.getInt("success") == 1) {

        Iterator iter = jObj.keys();
        while(iter.hasNext()){
        String key = (String)iter.next();
        String value = jObj.getString(key);
        array_list_values.add(value);
        }


        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, array_list_values));
                ListView listView = getListView();
            }
     }
catch (JSONException e) {
      Log.e(TAG, e.toString());
  }
于 2012-11-26T07:40:06.993 回答
0

你应该这样做并使用很棒的 GSON 库:

InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
APIResponse response = gson.fromJson(reader, APIResponse.class);

和:

Public class APIResponse{

public String 0;
public String 1;
public String 2;
public String 3;
public String 4;
public String 5;
public String succes;

}

但这不起作用,因为您需要为变量找到 0,1,2,3,4,5 的其他名称。

请在服务器端更改此设置

于 2012-11-26T07:39:43.737 回答
0
JSONObject jsonObject = new JSONObject(jsonString);
String value0 = jsonObject.getString("0");  

或在 for 循环中

String tempArray[] = new String[5];

做就是了

for(int i=0;condition;i++){
    tempArray[i] = jsonObject.getString(String.ValueOf(i));
}

并将数组传递给适配器

于 2012-11-26T07:40:37.680 回答