我有一个我想要显示的JSON数组。我能够成功地将其显示为文本视图,但并未列出所有数据。只有最后一个。这是“明天我们将在办公室赠送爆米花和更多的水瓶!星期二,我们将免费提供早餐!请在获得免费食品时注册成为CSA志愿者!”。我相信将它列为列表视图会更有效率,但是,我从在线网站上阅读的所有内容都无效。
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://ec2-54-213-155-95.us-west-2.compute.amazonaws.com/notices.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
try {
JSONObject jObject = new JSONObject(result);
JSONArray jsonArray = jObject.getJSONArray("notices");
for(int i = 0; i < jsonArray.length(); i++) {
String arrayString = jsonArray.getString(i);
Log.d("notices", arrayString);
ListView listView1 = (ListView) findViewById(R.id.listView1);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
基本上我想把它显示为列表视图!
答案 0 :(得分:1)
首先,您应该考虑在异步任务中执行此操作。
其次在lsitview中显示它相对容易。你可以这样做:
public class YourActivity extends Activiy{
public void getJson(String selection, String url) {
new LoadJsonTask().execute( selection, url);
}
private class LoadJsonTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {
ProgressDialog dialog ;
protected void onPreExecute (){
dialog = ProgressDialog.show(YourActivity.this ,"title","message");
}
protected ArrayList<HashMap<String, String>> doInBackground (String... params){
return doGetJson(params[0],params[1]);
}
protected void onPostExecute(ArrayList<HashMap<String, String>> mylist){
ListAdapter adapter = new JsonAdapter(YourActivity.this, mylist, R.layout.list,
new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,
R.id.item_subtitle, R.id.timestamp});
setListAdapter(adapter);
dialog.dismiss();
}
}
public ArrayList<HashMap<String, String>> doGetJson(String selection, String url) {
JSONObject json = null;
String formatedcat = selection.toLowerCase();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
json = JSONfunctions
.getJSONfromURL(url);
try {
//the array title that you parse
JSONArray category = json.getJSONArray(formatedcat);
for (int i = 0; i < category.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject c = category.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name",
c.getString("title"));
//map.put("text",c.getString("title"));
map.put("ts",c.getString("run_date") );
map.put("image","http:"+c.getString("url"));
mylist.add(map);
}
} catch (JSONException e) {
}
return mylist;
....
}
代码的重要部分是:
ListAdapter adapter = new JsonAdapter(YourActivity.this, mylist, R.layout.list,
new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,
R.id.item_subtitle, R.id.timestamp});
setListAdapter(adapter);
这是您将字符串数组设置为从json对象创建的hashmap并使用它设置列表适配器的位置,以便listview将显示所有相关数据。详情请见question。