内容无法显示,请老师帮我看看这个错误。
错误只有一条:
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,所以就改成了一个,但还是有问题。
正在回答
这个时候数据还是空的,所以显示不出来,需要先拿到数据再绑定适配器上,在获取数据的时候不能使用适配器,你可以在activity的 onResume() 回调方法里,完成数据及界面的更新,例如:
@Override protected void onResume() { super.onResume(); initData();// 获取数据 initView();//初始化控件设置适配器 }
老师我知道我最上面那个是怎么回事了,因为:
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>
请问以上问题如何解决?谢谢老师啦!
由于从子类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(); } }
图中有三条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; } }
主活动
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; } };}
子类
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; } }
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星