我正在尝试从数据库中填充列表视图。我有一个活动,我写了一个存储在数据库中的名称,我希望它显示在列表视图中。
这是我有列表视图的活动:
public class Liste extends ListActivity {
private ArrayList<String> results = new ArrayList<String>();
private String tableName = DBhelp.DATABASE_TABLE;
private SQLiteDatabase newDB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button bAdd = new Button(this);
bAdd.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(Liste.this , Add.class);
startActivity(i);
}
});
openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);
}
private void openAndQueryDatabase() {
try {
DBhelp dbHelper = new DBhelp(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("SELECT KEY_NAME " +
tableName +
" LIMIT 5", null);
if (c != null ) {
if (c.moveToLast()) {
do {
String firstName = c.getString(c.getColumnIndex("KEY_NAME"));
results.add(firstName);
}while (c.moveToPrevious());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
}
if (newDB != null)
newDB.execSQL("DELETE FROM " + tableName);
newDB.close();
}
}
这里我有数据库助手:
public class DBhelp {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
private static final String DATABASE_NAME = "DBhelpdb";
static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DBhelp(Context c){
ourContext = c;
}
public DBhelp open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public void createEntry(String name) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + " " + c.getString(iName) + "\n";
}
return result;
}
public String getName(long l) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if (c!=null){
c.moveToFirst();
String name = c.getString(1);
return name;
}
return null;
}
public void updateEntry(long lRow, String mName) {
// TODO Auto-generated method stub
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, mName);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + lRow, null);
}
public void deteleEntry(long lRow1) {
// TODO Auto-generated method stub
ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + lRow1, null);
}
public SQLiteDatabase getWritableDatabase() {
// TODO Auto-generated method stub
return null;
}
}
这是在数据库中插入行的活动:
public class Add extends Activity implements OnClickListener{
Button sqlUpdate;//, sqlView, sqlModify, sqlGetInfo, sqlDelete;
EditText sqlName;//, sqlHotness, sqlRow;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
sqlUpdate = (Button) findViewById(R.id.bFinishAdd);
//sqlView = (Button) findViewById(R.id.bSQLopenView);
sqlName = (EditText) findViewById(R.id.etAdd);
sqlUpdate.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean didItWork = true;
try{
String name = sqlName.getText().toString();
DBhelp entry = new DBhelp(Add.this);
entry.open();
entry.createEntry(name);
entry.close();
Intent i = new Intent(Add.this, Liste.class);
startActivity(i);
}catch(Exception i){
didItWork = false;
}finally{
}
}
});
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
当我启动应用程序时,它给了我一个 Error 。我真的不知道是哪个问题。如果您能提供建议或提示,如果您能告诉我问题出在哪里,甚至更好,我将不胜感激。很抱歉我的帖子这么长,但我想给你所有的细节。我真的很感谢你!
这是日志猫:
12-29 00:58:16.852: E/AndroidRuntime(456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-29 00:58:16.852: E/AndroidRuntime(456): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 00:58:16.852: E/AndroidRuntime(456): at android.os.Looper.loop(Looper.java:123)
12-29 00:58:16.852: E/AndroidRuntime(456): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-29 00:58:16.852: E/AndroidRuntime(456): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 00:58:16.852: E/AndroidRuntime(456): at java.lang.reflect.Method.invoke(Method.java:521)
12-29 00:58:16.852: E/AndroidRuntime(456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-29 00:58:16.852: E/AndroidRuntime(456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-29 00:58:16.852: E/AndroidRuntime(456): at dalvik.system.NativeStart.main(Native Method)
12-29 00:58:16.852: E/AndroidRuntime(456): Caused by: java.lang.NullPointerException
12-29 00:58:16.852: E/AndroidRuntime(456): at com.project.mapshop.Liste.openAndQueryDatabase(Liste.java:64)
12-29 00:58:16.852: E/AndroidRuntime(456): at com.project.mapshop.Liste.onCreate(Liste.java:42)
12-29 00:58:16.852: E/AndroidRuntime(456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 00:58:16.852: E/AndroidRuntime(456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)