listview - 将"JSON data to textview" 转换为"JSON data to listview"

  显示原文与译文双语对照的内容
0 0

你好,我有一个工作代码,它将一个 Twitter 时间轴显示到textview中:


 void examineJSONdata()
{
 TextView tvData = (TextView) findViewById(R.id.txtData);
 try
 {
 String x ="";
 InputStream is = this.getResources().openRawResource(R.raw.jsontwitter);
 byte [] buffer = new byte[is.available()];
 while (is.read(buffer)!= -1);
 String jsontext = new String(buffer);
 JSONArray entries = new JSONArray(jsontext);

 x ="JSON parsed.nThere are [" + entries.length() +"]nn";

 int i;
 for (i=0;i<entries.length();i++)
 {
 JSONObject post = entries.getJSONObject(i);
 x +="------------n";
 x +="Date:" + post.getString("created_at") +"n";
 x +="Post:" + post.getString("text") +"nn";
 }
 tvData.setText(x);
 }
 catch (Exception je)
 {
 tvData.setText("Error w/file:" + je.getMessage());
 }
}

现在我想尝试一个ListView而不是一个 TextView

我找到了 StackOverFlow Android 应用的代码: JSON array 从web到 listview


ListView list = (ListView) findViewById(...);
String json ="["Country1","Country2","Country3"]";
try {
 JSONArray array = (JSONArray) new JSONTokener(json).nextValue();

 String[] stringarray = new String[array.length()];
 for (int i = 0; i <array.length(); i++) {
 stringarray[i] = array.getString(i);
 }
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringarray); 
 list.setAdapter(adapter);
} catch (JSONException e) {
//handle JSON parsing exceptions...
}

我试图做到这一点,但我无法得到我的工作将有人帮助转换代码与我? 我想所有需要的信息都在第一个代码里。

时间: 原作者:

0 0

 String x ="";
 InputStream is = this.getResources().openRawResource(R.raw.jsontwitter);
 byte [] buffer = new byte[is.available()];
 while (is.read(buffer)!= -1);
 String jsontext = new String(buffer);
 JSONArray array = new JSONArray(jsontext);
 String[] stringarray = new String[array.length()];


int i;
 for (i=0;i<array.length();i++)
 {
 JSONObject post = array.getJSONObject(i);
 String temp;

 temp +="Date:" + post.getString("created_at") +"n";
 temp +="Post:" + post.getString("text");
 stringarray[i]=temp;
 }
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, stringarray); 
 list.setAdapter(adapter);

原作者:
...