这是我的代码
当我手动从Cursor检索数据时,结果很好, 但是当我创建SimpleCursorAdapter并将光标放入其中时,我给了我致命的异常
package com.example.dummy;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends Activity {
private static final String T = "Main";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Student s = new Student(this);
s.insert("Jeevan");
s.insert("Wahab");
s.insert("Majid");
Cursor c = s.getAllStudents();
ListView l = (ListView) findViewById(R.id.listView);
String[] from = {DbTable.NAME};
int[] to = {android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, c, from, to, 0);
l.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.example.dummy;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
public class Student {
private static final String T = "Student";
private DbTable dbHelper;
private SQLiteDatabase db;
public Student(Context context) {
dbHelper = new DbTable(context);
db = dbHelper.getWritableDatabase();
}
public long insert(String name) {
ContentValues cv = new ContentValues();
cv.put(DbTable.NAME, name);
long insert = 0;
try {
insert = db.insert(DbTable.TABLE, null, cv);
} catch (SQLiteException e) {
Log.d(T, "insert " + e.getLocalizedMessage());
}
return insert;
}
public Cursor getAllStudents(){
String[] columns = {DbTable.ID, DbTable.NAME};
Cursor c = db.query(DbTable.TABLE,
columns,
null, null, null, null, null);
if (c.moveToFirst() ) {
return c;
}
return c;
}
}
创建表格
package com.example.dummy;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DbTable extends SQLiteOpenHelper {
public static final String TABLE = "STUDENT";
public static final String ID = "ID";
public static final String NAME = "NAME";
private static final String T = "DATABASE TABLE";
private final String CREATE_TABLE = "CREATE TABLE "+TABLE
+ " ( '" + ID + "' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ "'"+NAME + "' VARCHAR NOT NULL );";
public DbTable(Context context) {
super(context, "MYDB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(CREATE_TABLE);
Log.i(T, "database created");
}catch(SQLiteException e){
Log.d(T, "error onCreate "+e.getLocalizedMessage());
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try{
db.execSQL("DROP TABLE IF NOT EXISTS "+TABLE);
onCreate(db);
Log.i(T, "onUpgrade ");
}catch(SQLiteException e){
Log.d(T, "onUpgrade "+e.getLocalizedMessage());
}
}
}