ListView中的动态分隔符

时间:2013-05-29 08:21:47

标签: android json android-layout android-listview expandablelistview

我想将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();
}

 }

我只想将列表视图与Menuname分开。每个Menuname应该出现在带有背景的某些textview上,然后它的各个子菜单必须显示在那个下面,然后是另一个主菜单,依此类推。请建议如何实现它以及如何修改适配器类。

1 个答案:

答案 0 :(得分:0)

您基本上想要做的是使用自定义适配器,很可能源自BaseAdapter。您需要覆盖getViewTypeCount()并返回列表中不同类型的列表项的数量。在你的情况下它是2,因为你有菜单名称和普通列表项。

如果指定位置的项目是普通列表项目,则还必须覆盖getItemViewType(position)并返回0;如果是菜单名称,则返回1。

最后,您还必须覆盖getView()并根据getItemViewType()返回相应类型(菜单名称或普通列表项)的列表项。

如果你有点谷歌,应该有几段代码可以做到这一点。我写了一个SectionedAdapter来做这件事。它是一个泛型类,它处理某种类型的部分/标题(可以是String或某些特定的类,如Section,Header或MenuName)和部分内容作为另一种类型。对它进行子类化并实现getSectionView()和getItemView()。使用addSection()和addItem()添加节和项。