Android 转换;JSON数据到文本视图;至;“将JSON数据发送到listview”;
您好,我有一个工作代码,可以在文本视图中显示twitter时间线:Android 转换;JSON数据到文本视图;至;“将JSON数据发送到listview”;,android,listview,twitter,textview,Android,Listview,Twitter,Textview,您好,我有一个工作代码,可以在文本视图中显示twitter时间线: void examineJSONdata() { TextView tvData = (TextView) findViewById(R.id.txtData); try { String x = ""; InputStream is = this.getResources().openRawResource(R.raw.jsontwitter);
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() + "]\n\n";
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") + "\n\n";
}
tvData.setText(x);
}
catch (Exception je)
{
tvData.setText("Error w/file: " + je.getMessage());
}
}
void-examineJSONdata()
{
TextView tvData=(TextView)findViewById(R.id.txtData);
尝试
{
字符串x=“”;
InputStream is=this.getResources().openRawResource(R.raw.jsontwitter);
byte[]buffer=新字节[is.available()];
while(is.read(buffer)!=-1);
String jsontext=新字符串(缓冲区);
JSONArray条目=新的JSONArray(jsontext);
x=“已解析JSON。\n有[“+条目的.length()+”]\n\n”;
int i;
对于(i=0;i
及
inti;
对于(i=0;i什么不起作用?listview不是显示了,还是空的,或者显示了错误的数据?它显示了什么?任何错误?任何崩溃?第一个代码起作用,但我想使用listview,所以我在另一个StackOverFlow问题中找到了第二个代码,我想使用它来显示我的数据,而不是那样所以我想如果有人能帮我重新格式化第二个版本中的代码,这样我就有了一个listview而不是TextView。我不完全理解你的意思,但我认为替换JSONArray数组=(JSONArray)新的JSONTokener(json).nextValue();
使用String x=”“;InputStream=this.getResources().openRawResource(R.raw.jsontwitter);byte[]buffer=new byte[is.available()];while(is.read(buffer)!=1);String jsontext=new String(buffer);JSONArray数组=new JSONArray(jsontext);
谁来解决您的问题第二个问题看不出有什么问题..您是否将contentView设置为listview?非常感谢,它正在工作,我只将String temp;更改为String temp=“”;所以它工作了,但我也喜欢,因为整个tweet都在显示,只显示1条规则。所以你看不到整个tweet只是一部分,当你点击它时,它会显示整个tweet。
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...
}
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);