Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView中的动态分隔符_Android_Json_Android Layout_Android Listview_Expandablelistview - Fatal编程技术网

Android ListView中的动态分隔符

Android ListView中的动态分隔符,android,json,android-layout,android-listview,expandablelistview,Android,Json,Android Layout,Android Listview,Expandablelistview,我想将json解析为列表视图 { "Submenus": [ { "MenuName": "Pizza", "SubMenu": [ "Pizza Margherita ", "Pizza al Prosciutto", "Pizza al Peperoncino " ] }, { "MenuName": "Bøffer",

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

 }
公共类菜单任务扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//TODO自动生成的方法存根
List params=new ArrayList();
//正在从URL获取JSON字符串。。。。。。。。。。。。。。
JSONObject JSONObject=jParser.makeHttpRequest(
"http://smartaway.dk/json/submenu.php?resid=“+res_id,
“POST”,params);
试一试{
bestdeal=jsonObject.getJSONArray(标记菜单);
///通过通道循环。。。。。。。。
for(int i=0;ivalue
//地图放置(标签标识,标识);
地图放置(标记名称,重新命名);
地图放置(标签价格,城市/州);
/*
*地图放置(TAG_STREET,STREET);地图放置(TAG_COUSINE,
*cosine);map.put(TAG_RES_LOGO,reslogo);
*/
//将哈希列表添加到ArrayList
最佳交易清单。添加(地图);
}
// }
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
@抑制警告(“弃用”)
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
runOnUiThread(新的Runnable(){
公开募捐{
MenuAdapter MenuAdapter=new MenuAdapter(RestaurantDetails.this.getParent(),bestdeal_列表);
//setListAdapter(菜单适配器);
list.setAdapter(menuAdapter);
}
});
}
// }
}
自定义适配器类如下所示

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

 }
公共类MenuAdapter扩展BaseAdapter{
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共菜单适配器(活动a,阵列列表d){
活动=a;
数据=d;
充气器=(充气器)活动
.getSystemService(上下文布局\充气机\服务);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回data.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图vi=转换视图;
if(convertView==null)
vi=充气机充气(R.layout.menu_list,空);
TextView菜单名=(TextView)vi.findViewById(R.id.textView1);//标题
TextView价格=(TextView)vi.findViewById(R.id.textView3);
HashMap事件=新建HashMap();
事件=数据。获取(位置);
String menustr=events.get(RestaurantDetails.TAG_MENUNAME);
String pricestr=events.get(RestaurantDetails.TAG_PRICE);
menuname.setText(menustr);
price.setText(pricestr);
返回vi;
}
@凌驾
public int getItemViewType(int位置){
//TODO自动生成的方法存根
返回super.getItemViewType(位置);
}
@凌驾
public int getViewTypeCount(){
//TODO自动生成的方法存根
返回super.getViewTypeCount();
}
}

我只想将listview与Menuame分开。每个菜单都应该出现在某个文本视图的背景上,然后它各自的子菜单必须显示在该文本视图下,然后是另一个主菜单,依此类推。请建议如何实现它以及如何修改适配器类

您基本上想要做的是使用一个自定义适配器,它很可能是从。您需要覆盖并返回列表中不同种类的列表项的数量。在你的例子中是2,因为你有菜单名和普通的列表项

如果指定位置的项是普通列表项,则必须重写并返回0;如果是菜单名,则必须重写并返回1

最后,还必须基于getItemViewType()重写并返回适当类型的列表项(菜单名或普通列表项)

如果你用谷歌搜索一下,应该有一些代码片段已经做到了这一点。我写了一篇文章,也是这样写的。它是一个泛型类,处理特定类型的节/标题(可以是字符串或某些特定类,如节、标题或MenuName)和另一种类型的节内容。子类化它并实现getSectionView()和getItemView()。使用addSection()和addItem()添加节和项