内容无法显示,请老师帮我看看这个错误。

内容无法显示,请老师帮我看看这个错误。

错误只有一条:

06-06 20:18:20.640 26672-26672/com.lexieluv.homeworkfourteenth2 E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 64 rows, 3 columns.

主活动代码如下:

package com.lexieluv.homeworkfourteenth2;

import android.content.ContentResolver;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.lexieluv.homeworkfourteenth2.bean.Children;
import com.lexieluv.homeworkfourteenth2.bean.Parent;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ExpandableListView exlv;
    private ExAdapter adapter;
    private List<Parent> pList = new ArrayList<>();
    private List<Children> cList = new ArrayList<>();
    Parent p;
    Children c;
    Uri uri = Uri.parse("content://com.imooc.menuprovider");
//    Uri uriParent = Uri.parse("content://com.imooc.menuprovider/type_tb");
//    Uri uriChild = Uri.parse("content://com.imooc.menuprovider/dish_tb");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        exlv = findViewById(R.id.exlv);

        //拿到ContentProvider的数据
        ContentResolver contentResolver = getContentResolver();
        pList = readData(contentResolver);
        //获取数据之后再利用适配器
        adapter = new ExAdapter(this,pList);
        exlv.setAdapter(adapter);
    }

    private List<Parent> readData(ContentResolver resolver) {
//        Cursor cursor = null;
//        Cursor cursor2 = null;
//        try{
//        cursor = getContentResolver().query(uriParent,null,null,null,null);
//        cursor2 = getContentResolver().query(uriChild,null,null,null,null);
//        if(cursor != null) {
//            while (cursor.moveToNext()) {
//                //获取父类,columnName应该使用bean类的final常量
//                String type_name = cursor.getString(cursor.getColumnIndex(Parent.TYPE_NAME));
//                int type_id = cursor.getInt(cursor.getColumnIndex(Parent.TYPE_ID));
//                p = new Parent(type_id, type_name);
//                pList.add(p);
//            }
//            adapter.notifyDataSetChanged();//适配器及时改变更新
//        }
//        if(cursor2 != null){
//            while (cursor2.moveToNext()){
//                //获取子类
//                String dish_name = cursor2.getString(cursor2.getColumnIndex(Children.DISH_NAME));
//                int dish_id = cursor2.getInt(cursor2.getColumnIndex(Children.DISH_ID));
//                String dish_type = cursor2.getString(cursor2.getColumnIndex(Children.DISH_TYPE));
//                c = new Children(dish_id,dish_name);
//                cList.add(c);
//            }
//            adapter.notifyDataSetChanged();
//        }
//    }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//        if(cursor != null){
//            cursor.close();
//            cursor2.close();
//        }
//        }
        //根据provider来只来绑定子类,请根据提供的代码变化
        Cursor cc = null;
        try{
            cc = resolver.query(uri,null,null,null,null);
            if(cc != null){
                while(cc.moveToNext()){
                String dish_name = cc.getString(cc.getColumnIndex(Children.DISH_NAME));
                int dish_id = cc.getInt(cc.getColumnIndex(Children.DISH_ID));
                String dish_type = cc.getString(cc.getColumnIndex(Children.DISH_TYPE));
                String type_name = cc.getString(cc.getColumnIndex(Parent.TYPE_NAME));
                int type_id = cc.getInt(cc.getColumnIndex(Parent.TYPE_ID));
                c = new Children(dish_id,dish_name);
                p = new Parent(type_id,type_name);
                cList.add(c);
                pList.add(p);
                }
                adapter.notifyDataSetChanged();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
        if(cc != null){
            cc.close();
        }
        }
        return pList;//返回父类列表,里面也包含了子类
    }


    //自定义长按监听类
private AdapterView.OnItemLongClickListener onItemLongClickListener = new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        final long packedPosition = exlv.getExpandableListPosition(position);
        final int parentPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
        final int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);

        //删除父项
        if(childPosition != -1){
            //显示消息框
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("警告");
            builder.setMessage("您在试图搞破坏,确定吗?");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //更新数据库
//                    Db db = new Db(context,"pwprotect",null,1);
//                    SQLiteDatabase sqldb = db.getWritableDatabase();
//                    int result = dbWrite.delete(type[groupPosition],"_id=?",new String[]{""+
//                            childs_id[groupPosition][childPosition]});
//                    dbWrite.close();
//                    if(result == 1){
//                        refrash();
//                        Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
//                    }
                }
            });
            builder.setNegativeButton("取消",null);
            builder.show();
        } else { //删除子项
            //显示消息框
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("警告");
            builder.setMessage("您在试图搞破坏,确定吗?");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //更新数据库
//                    Db db = new Db(context,"pwprotect",null,1);
//                    SQLiteDatabase sqldb = db.getWritableDatabase();
//                    int result = dbWrite.delete(type[groupPosition],"_id=?",new String[]{""+
//                            childs_id[groupPosition][childPosition]});
//                    dbWrite.close();
//                    if(result == 1){
//                        refrash();
//                        Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
//                    }
                }
            });
            builder.setNegativeButton("取消",null);
            builder.show();
        }
        return true;
    }
};}

我本来是创立两个cursor分开绑定数据,但是看到menudemo里面提供的方法只有dish_tb,所以就改成了一个,但还是有问题。

正在回答

登陆购买课程后可参与讨论,去登陆

8回答

这个时候数据还是空的,所以显示不出来,需要先拿到数据再绑定适配器上,在获取数据的时候不能使用适配器,你可以在activity的 onResume() 回调方法里,完成数据及界面的更新,例如:

@Override
protected void onResume() {
    super.onResume();
    initData();// 获取数据
    initView();//初始化控件设置适配器
}

http://img1.sycdn.imooc.com//climg/5cff2392000128af05770360.jpg

  • LexieMIZUKI 提问者 #1
    老师,还是不行555555555,但是有没有明显的蓝色文字的那种报错?怎么办呢?
    2019-06-11 20:08:51
  • irista23 回复 提问者 LexieMIZUKI #2
    建议你把作业提交上来,老师结合完整代码,帮您指出问题并给出解决方案。每个作业都有两次提交机会呢~
    2019-06-12 09:59:14
  • LexieMIZUKI 提问者 回复 irista23 #3
    好的,谢谢啦!
    2019-06-12 14:12:48
提问者 LexieMIZUKI 2019-06-13 14:41:08

老师我知道我最上面那个是怎么回事了,因为:

private void addData() {
    ContentValues values = new ContentValues();
    values.put("dish_type",spin.toString());
    values.put("dish_name",et_name.getText().toString());
    
    //根据uri向contentprovider中插入数据,不需要跳转界面获取,因为是插入表格中,直接到时候刷新adapter就行
    resolver.insert(uri,values);
}

这是我的addActivity,在values那里我直接用的表格的列名,然后可能系统没能识别,就变成了图中最上面那样的名字,因为我点开可以看到被添加的东西。

还有我不知道我的spinner数据获取的是不是有问题,我是直接在string里写的array,

<array name="dishArray">
    <item>凉菜</item>
    <item>创意菜</item>
    <item>小吃</item>
    <item>时令</item>
    <item>汤</item>
    <item>海鲜</item>
    <item>蒸菜</item>
    <item>面食</item>
</array>

请问以上问题如何解决?谢谢老师啦!

  • 不行,不要直接写死在程序中。你去查源数据时,表中就是分类这个字段,你把它保存下来。
    2019-06-13 17:38:43
  • 提问者 LexieMIZUKI 回复 好帮手慕雪 #2
    老师,还有下面那个问题也可以帮我看看吗,就是父类名字用的子类的dish_type,所以很多重复,怎么解决呢?
    2019-06-14 09:43:22
  • 好帮手慕雪 回复 提问者 LexieMIZUKI #3
    你在读取数据时,要放两个容器里,一个是菜品的分组,把它放在Set中,这样就避免重复了。另一个例如放在Map中,key放菜品的分组,value放菜
    2019-06-14 10:01:53
提问者 LexieMIZUKI 2019-06-13 14:30:16

http://img1.sycdn.imooc.com//climg/5d01ed15000186e310802160.jpg

由于从子类type获取,所以就有好多重复的名字,我试了一下在父类的bean中加了toequals那两个方法看看会不会自动过滤掉相同的名字,发现还是这样。

 Cursor cc = null;
        try{
            cc = resolver.query(uri,null,null,null,null);
            if(cc != null){
                while(cc.moveToNext()){
                String dish_name = cc.getString(cc.getColumnIndex(Children.DISH_NAME));
                int dish_id = cc.getInt(cc.getColumnIndex(Children.DISH_ID));
                String dish_type = cc.getString(cc.getColumnIndex(Children.DISH_TYPE));
//                String type_name = cc.getString(cc.getColumnIndex(Parent.TYPE_NAME));
//                int type_id = cc.getInt(cc.getColumnIndex(Parent.TYPE_ID));
                c = new Children(dish_id,dish_name);
                c.setpType(dish_type);
//                p = new Parent(type_id,type_name);
                cList.add(c);
                p = new Parent(dish_type);
                pList.add(p);
                Log.d("======2======",dish_name);
                Log.d("======3======",c.toString());
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
        if(cc != null){
            cc.close();
        }
        }


提问者 LexieMIZUKI 2019-06-11 10:49:57

http://img1.sycdn.imooc.com//climg/5cff169c0001ab2205890355.jpg

http://img1.sycdn.imooc.com//climg/5cff169e00014bb907590573.jpg

图中有三条log都是可以显示的

下面为适配器的代码:

package com.lexieluv.homeworkfourteenth2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lexieluv.homeworkfourteenth2.R;
import com.lexieluv.homeworkfourteenth2.bean.Children;
import com.lexieluv.homeworkfourteenth2.bean.Parent;

import java.util.ArrayList;
import java.util.List;

public class ExAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<Parent> pData;
    private List<Children> cData;
    private LayoutInflater inflater;

    public ExAdapter(Context context,List<Parent> pData,List<Children> cData) {
        this.context = context;
        this.pData = pData;
        this.cData = cData;
        inflater = LayoutInflater.from(context);//把实例化写在构造函数里
    }

    //父类的数量
    @Override
    public int getGroupCount() {
        return pData.size();
    }

    //子类的数量
    @Override
    public int getChildrenCount(int groupPosition) {
//        return pData.get(groupPosition).getcList().size();
        return cData.size();
    }

    //获取父类的对象
    @Override
    public Object getGroup(int groupPosition) {
        return pData.get(groupPosition);
    }

    //获取子类的对象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
//        return pData.get(groupPosition).getcList().get(childPosition);
        return cData.get(groupPosition);
    }

    //获取父类id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获取子类id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //父类视图绑定
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder pvh;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_parent,parent,false);
            pvh = new ParentViewHolder();
            pvh.iv_pic = convertView.findViewById(R.id.iv_pic);
            pvh.tv_parent = convertView.findViewById(R.id.tv_parent);
            convertView.setTag(pvh);
        } else {
            pvh = (ParentViewHolder) convertView.getTag();
        }
        pvh.tv_parent.setText(pData.get(groupPosition).getName());
        pvh.iv_pic.setSelected(isExpanded);//图片设置伸展
        return convertView;
    }
    //子类视图绑定
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder cvh;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_children,parent,false);
            cvh = new ChildViewHolder();
            cvh.tv_child = convertView.findViewById(R.id.tv_children);
            convertView.setTag(cvh);
        } else {
            cvh = (ChildViewHolder) convertView.getTag();
        }
//        cvh.tv_child.setText(pData.get(groupPosition).getcList().get(childPosition).getName());
        cvh.tv_child.setText(cData.get(groupPosition).getName());
        return convertView;
    }

    //子类是否可以被选择
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    //先创建父类和子类自定义视图
    public static class ParentViewHolder{
        TextView tv_parent;
        ImageView iv_pic;
    }
    public static class ChildViewHolder{
        TextView tv_child;
    }
}


irista23 2019-06-10 16:46:54

adapter的初始化是在readData()后面执行的,所以下图中调用adapter.notifyDataSetChanged();会报空指针,你把初始化放在此方法的前面

http://img1.sycdn.imooc.com//climg/5cfe18440001622310100540.jpg

http://img1.sycdn.imooc.com//climg/5cfe18380001622310100540.jpg

  • 提问者 LexieMIZUKI #1
    老师,我调整了一下位置,不报错了,但是还是无法显示内容,我打印了3条log都可以显示,所以我怀疑是不是适配器有问题,请老师帮我看看,在上面。
    2019-06-11 10:48:38
提问者 LexieMIZUKI 2019-06-10 15:33:20

主活动

package com.lexieluv.homeworkfourteenth2;

import android.content.ContentResolver;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.lexieluv.homeworkfourteenth2.bean.Children;
import com.lexieluv.homeworkfourteenth2.bean.Parent;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

   private ExpandableListView exlv;
   private ExAdapter adapter;
   private List<Parent> pList = new ArrayList<>();
   private List<Children> cList = new ArrayList<>();
   Parent p;
   Children c;
   Uri uri = Uri.parse("content://com.imooc.menuprovider");
//    Uri uriParent = Uri.parse("content://com.imooc.menuprovider/type_tb");
//    Uri uriChild = Uri.parse("content://com.imooc.menuprovider/dish_tb");

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       exlv = findViewById(R.id.exlv);

       //拿到ContentProvider的数据
       ContentResolver contentResolver = getContentResolver();
       cList = readData(contentResolver);
       //获取数据之后再利用适配器
       adapter = new ExAdapter(this,pList);
       exlv.setAdapter(adapter);
   }

   private List<Children> readData(ContentResolver resolver) {
//        Cursor cursor = null;
//        Cursor cursor2 = null;
//        try{
//        cursor = getContentResolver().query(uriParent,null,null,null,null);
//        cursor2 = getContentResolver().query(uriChild,null,null,null,null);
//        if(cursor != null) {
//            while (cursor.moveToNext()) {
//                //获取父类,columnName应该使用bean类的final常量
//                String type_name = cursor.getString(cursor.getColumnIndex(Parent.TYPE_NAME));
//                int type_id = cursor.getInt(cursor.getColumnIndex(Parent.TYPE_ID));
//                p = new Parent(type_id, type_name);
//                pList.add(p);
//            }
//            adapter.notifyDataSetChanged();//适配器及时改变更新
//        }
//        if(cursor2 != null){
//            while (cursor2.moveToNext()){
//                //获取子类
//                String dish_name = cursor2.getString(cursor2.getColumnIndex(Children.DISH_NAME));
//                int dish_id = cursor2.getInt(cursor2.getColumnIndex(Children.DISH_ID));
//                String dish_type = cursor2.getString(cursor2.getColumnIndex(Children.DISH_TYPE));
//                c = new Children(dish_id,dish_name);
//                cList.add(c);
//            }
//            adapter.notifyDataSetChanged();
//        }
//    }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//        if(cursor != null){
//            cursor.close();
//            cursor2.close();
//        }
//        }
       //根据provider来只来绑定子类,请根据提供的代码变化
       Cursor cc = null;
       try{
           cc = resolver.query(uri,null,null,null,null);
           if(cc != null){
               while(cc.moveToNext()){
               String dish_name = cc.getString(cc.getColumnIndex(Children.DISH_NAME));
               int dish_id = cc.getInt(cc.getColumnIndex(Children.DISH_ID));
               String dish_type = cc.getString(cc.getColumnIndex(Children.DISH_TYPE));
//                String type_name = cc.getString(cc.getColumnIndex(Parent.TYPE_NAME));
//                int type_id = cc.getInt(cc.getColumnIndex(Parent.TYPE_ID));
               c = new Children(dish_id,dish_name);
//                p = new Parent(type_id,type_name);
               cList.add(c);
//                pList.add(p);
               }
               adapter.notifyDataSetChanged();
           }
       }catch (Exception e){
           e.printStackTrace();
       }finally {
       if(cc != null){
           cc.close();
       }
       }
       return cList;//返回父类列表,里面也包含了子类
   }


   //自定义长按监听类
private AdapterView.OnItemLongClickListener onItemLongClickListener = new AdapterView.OnItemLongClickListener() {
   @Override
   public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
       final long packedPosition = exlv.getExpandableListPosition(position);
       final int parentPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
       final int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);

       //删除父项
       if(childPosition != -1){
           //显示消息框
           AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
           builder.setTitle("警告");
           builder.setMessage("您在试图搞破坏,确定吗?");
           builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   //更新数据库
//                    Db db = new Db(context,"pwprotect",null,1);
//                    SQLiteDatabase sqldb = db.getWritableDatabase();
//                    int result = dbWrite.delete(type[groupPosition],"_id=?",new String[]{""+
//                            childs_id[groupPosition][childPosition]});
//                    dbWrite.close();
//                    if(result == 1){
//                        refrash();
//                        Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
//                    }
               }
           });
           builder.setNegativeButton("取消",null);
           builder.show();
       } else { //删除子项
           //显示消息框
           AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
           builder.setTitle("警告");
           builder.setMessage("您在试图搞破坏,确定吗?");
           builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                   //更新数据库
//                    Db db = new Db(context,"pwprotect",null,1);
//                    SQLiteDatabase sqldb = db.getWritableDatabase();
//                    int result = dbWrite.delete(type[groupPosition],"_id=?",new String[]{""+
//                            childs_id[groupPosition][childPosition]});
//                    dbWrite.close();
//                    if(result == 1){
//                        refrash();
//                        Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();
//                    }
               }
           });
           builder.setNegativeButton("取消",null);
           builder.show();
       }
       return true;
   }
};}


提问者 LexieMIZUKI 2019-06-10 15:32:44

http://img1.sycdn.imooc.com//climg/5cfe072900018d5913660768.jpg

子类

package com.lexieluv.homeworkfourteenth2.bean;


public class Children {
    private int id;
    private String name;
    private int pid;

    public static final String TABLE_NAME = "dish_tb";
    public static final String DISH_ID = "dish_id";
    public static final String DISH_NAME = "dish_name";
    public static final String DISH_TYPE = "dish_type";

    public Children() {
    }

    public Children(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }
}

父类

package com.lexieluv.homeworkfourteenth2.bean;

import java.util.ArrayList;
import java.util.List;

public class Parent {
    private int id;
    private String name;

    public static final String TABLE_NAME = "type_tb";
    public static final String TYPE_ID = "type_id";
    public static final String TYPE_NAME = "type_name";

    private List<Children> cList = new ArrayList<>();

    public Parent() {
    }

    public Parent(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Children> getcList() {
        return cList;
    }

    //增加子项的两种方法
    public void addChild(Children c){
        c.setPid(getId());
        cList.add(c);
    }
    public void addChild(int id,String name){
        Children c = new Children(id,name);
        c.setPid(getId());
        cList.add(c);
    }
}

适配器

package com.lexieluv.homeworkfourteenth2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lexieluv.homeworkfourteenth2.R;
import com.lexieluv.homeworkfourteenth2.bean.Children;
import com.lexieluv.homeworkfourteenth2.bean.Parent;

import java.util.List;

public class ExAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<Parent> pData;
    private LayoutInflater inflater;

    public ExAdapter(Context context, List<Parent> pData) {
        this.context = context;
        this.pData = pData;
        inflater = LayoutInflater.from(context);//把实例化写在构造函数里
    }

    //父类的数量
    @Override
    public int getGroupCount() {
        return pData.size();
    }

    //子类的数量
    @Override
    public int getChildrenCount(int groupPosition) {
        return pData.get(groupPosition).getcList().size();
    }

    //获取父类的对象
    @Override
    public Object getGroup(int groupPosition) {
        return pData.get(groupPosition);
    }

    //获取子类的对象
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return pData.get(groupPosition).getcList().get(childPosition);
    }

    //获取父类id
    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    //获取子类id
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    //父类视图绑定
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder pvh;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_parent,parent,false);
            pvh = new ParentViewHolder();
            pvh.iv_pic = convertView.findViewById(R.id.iv_pic);
            pvh.tv_parent = convertView.findViewById(R.id.tv_parent);
            convertView.setTag(pvh);
        } else {
            pvh = (ParentViewHolder) convertView.getTag();
        }
        pvh.tv_parent.setText(pData.get(groupPosition).getName());
        pvh.iv_pic.setSelected(isExpanded);//图片设置伸展
        return convertView;
    }
    //子类视图绑定
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder cvh;
        if(convertView == null){
            convertView = inflater.inflate(R.layout.item_children,parent,false);
            cvh = new ChildViewHolder();
            cvh.tv_child = convertView.findViewById(R.id.tv_children);
            convertView.setTag(cvh);
        } else {
            cvh = (ChildViewHolder) convertView.getTag();
        }
        cvh.tv_child.setText(pData.get(groupPosition).getcList().get(childPosition).getName());
        return convertView;
    }

    //子类是否可以被选择
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    //先创建父类和子类自定义视图
    public static class ParentViewHolder{
        TextView tv_parent;
        ImageView iv_pic;
    }
    public static class ChildViewHolder{
        TextView tv_child;
    }
}


irista23 2019-06-08 10:44:12

错误提示你访问的列-1超出有效范围了,你可以断点跟踪下,应该是红框部分的取值不对,数据表只要取得id、name、type即可。type列值有了,可以直接构建两个集合,一个是类型集合,另一个菜品集合。

http://img1.sycdn.imooc.com//climg/5cfb20970001bbb613840600.jpg

  • 提问者 LexieMIZUKI #1
    是的,我建立了两个集合,就在你红框下方可以看到分别有cList和pList
    2019-06-08 17:50:13
  • irista23 回复 提问者 LexieMIZUKI #2
    你断点跟踪就知道哪个列取值时超出范围了,建议你把红框代码去掉,构造的两个集合一个是所有菜品数据,一个是type的List<String>,这样传入适配器时,就可以根据type的List<String>取到相应菜品数据集合。
    2019-06-08 22:44:33
  • 提问者 LexieMIZUKI #3
    老师,我把那个添加到父类list的给注释掉了,而且原来readData返回的是父类的list现在改为子类的list,但是却显示空指针错误,我把完整项目代码和错误都放在上面吧,麻烦老师帮我看看!谢谢了!
    2019-06-10 15:30:22
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Android网络操作与数据存储2018版
  • 参与学习           人
  • 提交作业       307    份
  • 解答问题       1613    个

本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师