我正在使用 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 个列表项。我不想要可扩展的列表视图。只想修改上面提到的代码。