我成功解析了此website here中的数据。写完几个代码后,我得到一个字符串,我想在其中显示一个ListView。基本上,我想将整个数组从网站显示为ListView。
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();
}
我正在考虑使用arrayAdapter,但我不确定如何使用它!
答案 0 :(得分:4)
首先确定是否要在ListView中显示数据之前将数据存储在ArrayList或数据库中。对于ArrayList到ListView,您将需要ArrayAdapter,从数据库到ListView,您将需要CursorAdapter。 在ArrayList中,如果您只有TextView,则可以使用Simple ArrayAdapter else,如果有多个TextView或更多组件,则转到Custom ArrayAdapter。
答案 1 :(得分:0)
您可以按如下方式使用SimpleArrayAdapter: -
ArrayList<String> list = new ArrayList<String>();
for(int i = 0; i < jsonArray.length(); i++) {
String arrayString = jsonArray.getString(i);
Log.d("notices", arrayString);
list .add(arrayString);
}
// and after filling this array list you can set this adapter to you list as
ListView listView1 = (ListView) findViewById(R.id.listView1);
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, list);
listView1 .setAdapter(adapter)
答案 2 :(得分:0)
如果您想在listview中显示数据,那么您可以使用以下代码:
private ArrayAdapter<String> mArrayAdapter;
ListView listView1 ;
// Initialize array adapter.
mArrayAdapter = new ArrayAdapter<String>(this, R.layout.array_layout);
listView1 = (ListView) findViewById(R.id.listView1);
// Rest of your code...
//
//
try {
JSONObject jObject = new JSONObject(result);
JSONArray jsonArray = jObject.getJSONArray("notices");
for(int i = 0; i < jsonArray.length(); i++) {
String arrayString = jsonArray.getString(i);
mArrayAdapter.add(arrayString) ;
Log.d("notices", arrayString);
}
listView1.setAdapter(mArrayAdapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
<强> array_layout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="5dp"
/>
答案 3 :(得分:0)
尝试使用ArrayList<String>
和StableArrayAdapter
添加数据,如下所示:
ListView listView1 = (ListView) findViewById(R.id.listView1); JSONObject jObject = new JSONObject(result); JSONArray jsonArray = jObject.getJSONArray("notices"); final ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < jsonArray.length; ++i) { String arrayString = jsonArray.getString(i); Log.d("notices", arrayString); list.add(arrayString); } final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list); listview1.setAdapter(adapter);
StableArrayAdapter类
private class StableArrayAdapter extends ArrayAdapter<String> { HashMap<String, Integer> mIdMap = new HashMap<String, Integer>(); public StableArrayAdapter(Context context, int textViewResourceId, List<String> objects) { super(context, textViewResourceId, objects); for (int i = 0; i < objects.size(); ++i) { mIdMap.put(objects.get(i), i); } } @Override public long getItemId(int position) { String item = getItem(position); return mIdMap.get(item); } @Override public boolean hasStableIds() { return true; }
}
答案 4 :(得分:0)
到目前为止你可能已经得到了答案,但它仍然可以帮助别人。 我是这样做的,效果很好。
我制作了一个包含4个文本视图的xml recco_list文件。
声明这些。
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
ListView list;
进入数组
for(int i = 0; i < RECCO_ARRAY.length(); i++) {
try {
JSONObject recco = RECCO_ARRAY.getJSONObject(i);
JSONObject program = recco.getJSONObject("program");
JSONObject outlet = recco.getJSONObject("outlet");
String normalized_weight = recco.getString("normalized_weight");
String distance = recco.getString("distance");
HashMap<String, String> map = new HashMap<String, String>();
String program_name = program.getString("name");
String outlet_basics = outlet.getString("basics");
String outlet_name = new JSONObject(outlet_basics).getString("name");
map.put("program_name", program_name);
map.put("outlet_name", outlet_name);
map.put("normalized_weight", normalized_weight);
map.put("distance", distance);
oslist.add(map);
list= (ListView) rootView.findViewById(R.id.recco_list);
ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
R.layout.recco_list_view, new String[] {"program_name","outlet_name", "normalized_weight", "distance"},
new int[]{R.id.program_name,R.id.outlet_name, R.id.normalized_weight, R.id.distance});
list.setAdapter(adapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}