我使用AsynTask显示json在listview中的数据。列表视图中的“分隔符”
代码在这里。
public class MenuTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
List<NameValuePair> params = new ArrayList<NameValuePair>();
// Getting JSON String from URL..............
JSONObject jsonObject = jParser.makeHttpRequest(
"http://smartaway.dk/json/submenu.php?resid=" + res_id,
"POST", params);
try {
bestdeal = jsonObject.getJSONArray(TAG_MENU);
///LOOping through AllEvents........
for (int i = 0; i < bestdeal.length(); i++) {
JSONObject e = bestdeal.getJSONObject(i);
String resname = e.getString(TAG_MENUNAME);
String city_state = e.getString(TAG_PRICE);
// Creating New HAsh Map.........
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(TAG_ID, id);
map.put(TAG_MENUNAME, resname);
map.put(TAG_PRICE, city_state);
/*
* map.put(TAG_STREET, street); map.put(TAG_COUSINE,
* cousine); map.put(TAG_RES_LOGO, reslogo);
*/
// adding HashList to ArrayList
bestdeal_list.add(map);
}
// }
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
/*
* if(bestdeal_list.isEmpty()){ AlertDialog alertDialog=new
* AlertDialog.Builder(getParent()).create();
* alertDialog.setTitle("No Best Deal Found");
* alertDialog.setButton("Ok", new DialogInterface.OnClickListener()
* {
*
* @Override public void onClick(DialogInterface dialog, int which)
* {
*
*
* } }); alertDialog.show(); } else{
*/
/*
* if (bestdeal_list.isEmpty()) {
* Toast.makeText(getApplicationContext(), "Empty Menu",
* Toast.LENGTH_LONG).show(); } else{
*/
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
RestaurantDetails.this, bestdeal_list,
R.layout.menu_list, new String[] { TAG_MENUNAME,
TAG_PRICE }, new int[] { R.id.textView1,
R.id.textView3 });
list.setAdapter(adapter);
}
});
}
// }
}
所有的东西都工作正常,但我想通过将列表视图段修改我的代码。我想要在类别1下的前4个列表项目和类别2下的其他4个列表项目。我不想要可扩展列表视图。只是想修改上面提到的代码。
您可以通过实现您的自定义适配器来实现。 http://stackoverflow.com/a/1606642/2410326 – ooops
http://stackoverflow.com/questions/2394514/how-to-generate-a-listview-with-headers-above-some-sections –