2

我想解析json到列表視圖。ListView中的動態分隔符

{ 
"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(); 
} 

} 

我只是想從Menuname中分開listview。每個Menuname應該出現在帶有背景的某個文本視圖上,那麼它的各個子菜單必須顯示在該下面,然後是另一個主菜單,依此類推。請建議如何實現以及如何修改適配器類。

回答

0

你基本上想要做的是使用自定義適配器,最有可能來自BaseAdapter。您將需要覆蓋getViewTypeCount()並返回列表中不同種類的列表項的數量。你的情況是2,因爲你有菜單名稱和普通列表項。

您還必須重寫getItemViewType(position),如果指定位置的項目是普通列表項目,則返回0,如果是菜單名稱,則返回1。

最後,您還必須重寫getView(),並根據getItemViewType()返回適當類型(菜單名稱或普通列表項)的列表項。

應該有幾個代碼片段已經做到這一點,如果你有一點谷歌。我寫了一個SectionedAdapter,這也是如此。它是一個泛型類,它處理特定類型的段/標題(可以是字符串或某些特定的類,如Section,Header或MenuName)以及段內容作爲另一種類型。對它進行子類化並實現getSectionView()和getItemView()。使用addSection()和addItem()添加節和項目。