问题描述
- android studiod 从数据库读取数据失败
-
从数据库读取数据到listview,但是不知为什么,在没添加数据进数据库的时候,能进activity,但是一旦添加进去以后就开始报错了
DBhelper.java
public class DBHelper extends SQLiteOpenHelper{
private final static String DATABASE_NAME = "demo.db";
private final static int DATABASE_VERSION = 1;private final static String TABLE_NAME = "request_list"; public final static String REQUEST_ID = "request_id"; public final static String REPORTER_NAME = "reporter_name"; public final static String S_DESCRIPTION = "s_description"; public final static String L_DESCRIPTION = "l_description"; public final static String DEPARTMENT = "department"; public final static String CATEGORY = "category"; public DBHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); Log.e("Database operations", "Database created/opened"); } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE " + TABLE_NAME + " (" + REQUEST_ID + " INTEGER primary key autoincrement, " + "" + REPORTER_NAME + " text, " + "" + S_DESCRIPTION + " text, " + "" + L_DESCRIPTION + " text, " + "" + DEPARTMENT + " text, " + "" + CATEGORY + " text);"; db.execSQL(sql); Log.e("Database operations", "Table created/opened"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; db.execSQL(sql); onCreate(db); } public Cursor select() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db .query(TABLE_NAME, null, null, null, null, null, null); return cursor; } public Cursor getInformation(SQLiteDatabase db) { Cursor cursor; String[] projection = {REPORTER_NAME,S_DESCRIPTION,L_DESCRIPTION,DEPARTMENT,CATEGORY}; cursor=db.query(TABLE_NAME,projection,null,null,null,null,null,null); return cursor; } //增加操作 public long insert(String s_description,String name,String department, String category,String l_description) { SQLiteDatabase db = this.getWritableDatabase();
/* ContentValues */
ContentValues cv = new ContentValues();
cv.put(S_DESCRIPTION, s_description);
cv.put(L_DESCRIPTION,l_description);
cv.put(REPORTER_NAME,name);
cv.put(DEPARTMENT,department);
cv.put(CATEGORY,category);long row = db.insert(TABLE_NAME, null, cv); return row; } //删除操作 public void delete(int id) { SQLiteDatabase db = this.getWritableDatabase(); String where = REQUEST_ID + " = ?"; String[] whereValue ={ Integer.toString(id) }; db.delete(TABLE_NAME, where, whereValue); } //修改操作 public void update(int id, String s_description,String name,String department, String category,String l_description) { SQLiteDatabase db = this.getWritableDatabase(); String where = REQUEST_ID + " = ?"; String[] whereValue = { Integer.toString(id) }; ContentValues cv = new ContentValues(); cv.put(S_DESCRIPTION, s_description); cv.put(L_DESCRIPTION,l_description); cv.put(REPORTER_NAME,name); cv.put(DEPARTMENT,department); cv.put(CATEGORY,category); db.update(TABLE_NAME, cv, where, whereValue); }
}
DataListActivity.java
public class DataListActivity extends ActionBarActivity {
ListView listView;
SQLiteDatabase sqLiteDatabase;
DBHelper dbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.data_list_layout); listView = (ListView) findViewById(R.id.list_view); listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.reporter_my_request); listView.setAdapter(listDataAdapter); dbHelper = new DBHelper(getApplicationContext()); sqLiteDatabase = dbHelper.getReadableDatabase(); cursor = dbHelper.getInformation(sqLiteDatabase); if(cursor.moveToFirst()) { do { String name,title,department,category,description; name = cursor.getString(0); title= cursor.getString(1); department=cursor.getString(2); category=cursor.getString(3); description=cursor.getString(4); DataProvider dataProvider=new DataProvider(name,title,department,category,description); listDataAdapter.add(dataProvider); }while (cursor.moveToNext()); } dbHelper.close(); } } DataProvider.java public class DataProvider { private String name; private String title; private String department; private String category; private String description; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public DataProvider(String name,String title,String department,String category,String description){ this.name= name; this.title = title; this.department= department; this.category= category; this.description = description; }
}
ListDataAdapter.java
public class ListDataAdapter extends ArrayAdapter {
List list = new ArrayList();
public ListDataAdapter(Context context, int resource) {
super(context, resource);} static class LayoutHandler{ TextView NAME,TITLE,DEPARTMENT,CATEGORY,DESCRIPTION; } @Override public void add(Object object) { super.add(object); list.add(object); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public View getView(int position, View convertView, ViewGroup parent) { View row = convertView; LayoutHandler layoutHandler; if(row == null){ LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = layoutInflater.inflate(R.layout.reporter_my_request,parent,false); layoutHandler = new LayoutHandler(); layoutHandler.NAME = (TextView)row.findViewById(R.id.text_name); layoutHandler.TITLE = (TextView)row.findViewById(R.id.text_title); layoutHandler.DEPARTMENT = (TextView)row.findViewById(R.id.text_department); layoutHandler.CATEGORY = (TextView)row.findViewById(R.id.text_category); row.setTag(layoutHandler); } else { layoutHandler = (LayoutHandler)row.getTag(); } DataProvider dataProvider = (DataProvider)this.getItem(position); layoutHandler.NAME.setText(dataProvider.getName()); layoutHandler.TITLE.setText(dataProvider.getTitle()); layoutHandler.DEPARTMENT.setText(dataProvider.getDepartment()); layoutHandler.CATEGORY.setText(dataProvider.getCategory()); layoutHandler.DESCRIPTION.setText(dataProvider.getDescription()); return row; }
}
解决方案
至少把报错的信息贴出来啊,你这么一大段代码贴上来怎么帮你看
时间: 2024-09-15 11:43:52