以下是调查应用程序中Andoird中SQLite的数据库。每次调查都是一个存储在数据库中的记录。
我已成功创建数据库(并且数据库存在 - 我使用SQLite浏览器浏览所有不同的记录)。
但是,在进行一些调查后,我有一个问题显示所有记录。只有第一次调查才会显示在活动中。即使它们存在于内存中,也不会显示其他记录。我的简单游标适配器有问题吗?
这是我的数据库类:
package nidhin.survey;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Database extends Activity {
private static final SimpleCursorAdapter SimpleCursorAdapter = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
ListView lv=(ListView)findViewById(R.id.mylist);
lv.setClickable(true);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(Database.this,MainMenu.class);
startActivity(intent);
}
});
Intent sender=getIntent();
DatabaseHelper dbh = new DatabaseHelper(this);
Cursor c = dbh.getReadableDatabase().rawQuery("SELECT _id, " +
DatabaseHelper.COL_NAME +
", " + DatabaseHelper.VALUE1 +
", " + DatabaseHelper.VALUE2 +
" FROM " +
DatabaseHelper.TABLE_NAME, null); // initializing
String[] dataFrom ={DatabaseHelper.COL_NAME, DatabaseHelper.VALUE1, DatabaseHelper.VALUE2};//, DatabaseHelper.VALUE3, DatabaseHelper.VALUE4, DatabaseHelper.VALUE5, DatabaseHelper.VALUE6, DatabaseHelper.VALUE7, DatabaseHelper.VALUE8, DatabaseHelper.VALUE9, DatabaseHelper.VALUE10, DatabaseHelper.VALUE11};
int[] dataTo = {R.id.name, R.id.value1, R.id.value2};//, R.id.value3, R.id.value4, R.id.value5, R.id.value6, R.id.value7, R.id.value8, R.id.value9, R.id.value10, R.id.value11};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row, c, dataFrom, dataTo);
lv.setAdapter(adapter);
}
public void onListItemClick()
{
}
}
数据库助手:
package nidhin.survey;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper {
static final String TABLE_NAME = "mygrades";
static final String COL_NAME = "name";
static final String VALUE1 = "ClassNumber";
static final String VALUE2 = "value2";
static final String VALUE3 = "value3";
static final String VALUE4 = "value4";
static final String VALUE5 = "value5";
static final String VALUE6 = "value6";
static final String VALUE7 = "value7";
static final String VALUE8 = "value8";
static final String VALUE9 = "value9";
static final String VALUE10 = "value10";
static final String VALUE11 = "value11";
DatabaseHelper(Context context)
{
super(context, "grades.sqlite", null, 20);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE " + TABLE_NAME +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_NAME + " TEXT, " +
VALUE1 + " TEXT, " +
VALUE2 + " TEXT, " +
VALUE3 + " TEXT, " +
VALUE4 + " TEXT, " +
VALUE5 + " TEXT, " +
VALUE6 + " TEXT, " +
VALUE7 + " TEXT, " +
VALUE8 + " TEXT, " +
VALUE9 + " TEXT, " +
VALUE10 + " TEXT, " +
VALUE11 + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
android.util.Log.w("cars", "Upgrading database, all table info will be lost");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
答案 0 :(得分:0)
让我们再试一次
试试这个:
在数据库助手中(注意应该有5个空值):
public Cursor fetchAllSurveys() {
return mDb.query(TABLE_NAME, new String[] { "_id", COL_NAME, VALUE1,VALUE2 }, null, null, null, null, null);
}
在你的onCreate中,它会像这样使用:
Cursor c = dbh.fetchAllSurveys();
String[] dataFrom ={DatabaseHelper.COL_NAME, DatabaseHelper.VALUE1, DatabaseHelper.VALUE2};
int[] dataTo = {R.id.name, R.id.value1, R.id.value2};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, c, dataFrom, dataTo);
lv.setAdapter(adapter);
希望这有帮助!
答案 1 :(得分:0)
调用Cursor.moveToFirst,如:
if (c !=null)
{
c.moveToFirst();
}
在分配此行之前:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row, c, dataFrom, dataTo);