我使用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 –