我想将 json 解析为列表视图。
{
"Submenus": [
{
"MenuName": "Pizza",
"SubMenu": [
"Pizza Margherita ",
"Pizza al Prosciutto",
"Pizza al Peperoncino "
]
},
{
"MenuName": "Bøffer",
"SubMenu": [
"Oksekødmørbrad"
]
},
{
"MenuName": "Dessert",
"SubMenu": [
"Chokolade kage"
]
}
]
}
我的课如下。
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);
runOnUiThread(new Runnable() {
public void run() {
MenuAdapter menuAdapter=new MenuAdapter(RestaurantDetails.this.getParent(),bestdeal_list);
//setListAdapter(menuAdapter);
list.setAdapter(menuAdapter);
}
});
}
// }
}
自定义适配器类如下。
public class MenuAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public MenuAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.menu_list, null);
TextView menuname = (TextView) vi.findViewById(R.id.textView1); // title
TextView price = (TextView) vi.findViewById(R.id.textView3);
HashMap<String, String> events = new HashMap<String, String>();
events = data.get(position);
String menustr = events.get(RestaurantDetails.TAG_MENUNAME);
String pricestr = events.get(RestaurantDetails.TAG_PRICE);
menuname.setText(menustr);
price.setText(pricestr);
return vi;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return super.getItemViewType(position);
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return super.getViewTypeCount();
}
}
我只想将列表视图与菜单名分开。每个菜单名应该出现在一些带有背景的文本视图上,然后必须在其下显示其各自的子菜单,然后是另一个主菜单,依此类推。请建议如何实现以及如何修改适配器类。