Android SQLite:动态加载微调器
我已经尝试了几种从 SQLite 数据库加载微调器的不同方法,这种方法似乎是最简单和最容易的。唯一的事情是因为我添加了几个我自己的变量(另外三个)。当我运行编辑后的版本以满足我的测试需要时,除了微调器没有填充数据库中的数据
SpinnerLoad 类
public class SpinnerLoad extends Activity implements OnClickListener {
private Db thisTestDBAdapter;
Button save;
EditText one, two, three;
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerform);
save = (Button) findViewById(R.id.spinLoad_save);
save.setOnClickListener(this);
one = (EditText) findViewById(R.id.spinLoad_serial);
two = (EditText) findViewById(R.id.spinLoad_name);
three = (EditText) findViewById(R.id.spinLoad_place);
fillData();
spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
try {
spinner.setOnItemSelectedListener(new Person());
} catch (Exception e) {
e.printStackTrace();
}
}
public static class Person implements OnItemSelectedListener {
static String personReturn;
static boolean personTest = false;
@Override
public void onItemSelected(AdapterView<?> x, View y, int z, long w) {
personReturn = (x.getItemAtPosition(z)).toString();
personTest = true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
personTest = false;
}
}
private void fillData() {
try {
Cursor coloursCursor;
spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
coloursCursor = thisTestDBAdapter.fetchAllColours();
startManagingCursor(coloursCursor);
String[] from = new String[] { thisTestDBAdapter.KEY_TWO };
int[] to = new int[] { R.id.tvDBViewRow };
SimpleCursorAdapter colourAdapter = new SimpleCursorAdapter(this,
R.layout.db_view_row, coloursCursor, from, to);
spinner.setAdapter(colourAdapter);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.spinLoad_save:
String bx = two.getText().toString();
/**/
String ax = one.getText().toString();
String cx = three.getText().toString();
String returned = Person.personReturn;
Db entry = new Db(this);
entry.open();
entry.createEntry(ax, bx, cx, returned);
entry.close();
fillData();
/**/
break;
}
}
}
Db SQL 帮助程序类
public class Db {
private static final String TAG = "SpinnerDBHelper";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
/**/
private static final String KEY_DB = "DataBaseName";
private static final String KEY_TABLE = "DbTable";
public static final String KEY_ROWID = "_rowid"; // 0
public static final String KEY_ONE = "serial";
public static final String KEY_TWO = "name";
public static final String KEY_THREE = "place";
public static final String KEY_FOUR = "returned";
/**/
private static final String DATABASE_CREATE = "CREATE TABLE DbTable (_rowid INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "serial TEXT NOT NULL, "+ "name TEXT NOT NULL, " + "place TEXT NOT NULL, " + "returned TEXT NOT NULL);";
public Db(Context ctx) {
this.mCtx = ctx;
}
public Db open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createEntry(String ax, String bx, String cx, String returned) {
if (mDb == null) {
this.open();
}
ContentValues cv = new ContentValues();
cv.put(KEY_ONE, ax);
cv.put(KEY_TWO, bx);
cv.put(KEY_THREE, cx);
cv.put(KEY_FOUR, returned);
return mDb.insert(KEY_TABLE, null, cv);
}
public boolean deleteEntry(long rowId) {
return mDb.delete(KEY_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public boolean deleteAll() {
if (mDb == null) {
this.open();
}
return mDb.delete(KEY_TABLE, null, null) > 0;
}
public Cursor fetchAllColours() {
if (mDb == null) {
this.open();
}
return mDb.query(KEY_TABLE, new String[] { KEY_ROWID, KEY_TWO},
null, null, null, null, null);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, KEY_DB, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + KEY_TABLE);
onCreate(db);
}
}
}
LOGCAT
12-26 23:39:33.409: W/System.err(30879): java.lang.NullPointerException 12-26 23:39:33.409: W/System.err(30879): 在 com.my.examples.SpinnerLoad.fillData(SpinnerLoad.java:59) 12-26 23:39:33.409: W/System.err(30879): 在 com.my.examples.SpinnerLoad.onCreate(SpinnerLoad.java:30) 12-26 23:39:33.418:W / System.err(30879):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 12-26 23:39:33.418:W / System.err(30879):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 12-26 23:39:33.418:W / System.err(30879):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 12-26 23:39:33.418: W/System.err(30879): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 12-26 23:39:33.418: W/System.err(30879): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 12-26 23:39:33.418: W/System.err(30879): 在 android.os.Handler.dispatchMessage(Handler.java:99) 12-26 23:39:33.418: W/System.err(30879): 在 android.os.Looper.loop(Looper.java:123) 12-26 23:39:33.418:W / System.err(30879):在android.app.ActivityThread.main(ActivityThread.java:4627) 12-26 23:39:33.428:W / System.err(30879):在java.lang.reflect.Method.invokeNative(本机方法) 12-26 23:39:33.428:W / System.err(30879):在java.lang.reflect.Method.invoke(Method.java:521) 12-26 23:39:33.428: W/System.err(30879): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-26 23:39:33.428: W/System.err(30879): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-26 23:39:33.428:W/System.err(30879):在dalvik.system.NativeStart.main(本机方法) 12-26 23:40:00.628: E/Database(30879): 插入地点=zyxwvutsr 时出错返回=null 序列=123456789 名称=abcdefghij 12-26 23:40:00.628: E/Database(30879): android.database.sqlite.SQLiteConstraintException: 错误代码 19: 约束失败 12-26 23:40:00.628:E /数据库(30879):在android.database.sqlite.SQLiteStatement.native_execute(本机方法) 12-26 23:40:00.628:E /数据库(30879):在android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55) 12-26 23:40:00.628:E /数据库(30879):在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549) 12-26 23:40:00.628:E /数据库(30879):在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410) 12-26 23:40:00.628:E /数据库(30879):在com.my.examples.Db.createEntry(Db.java:56) 12-26 23:40:00.628:E /数据库(30879):在com.my.examples.SpinnerLoad.onClick(SpinnerLoad.java:86) 12-26 23:40:00.628:E /数据库(30879):在android.view.View.performClick(View.java:2408) 12-26 23:40:00.628: E/Database(30879): 在 android.view.View$PerformClick.run(View.java:8816) 12-26 23:40:00.628:E /数据库(30879):在android.os.Handler.handleCallback(Handler.java:587) 12-26 23:40:00.628:E /数据库(30879):在android.os.Handler.dispatchMessage(Handler.java:92) 12-26 23:40:00.628:E /数据库(30879):在android.os.Looper.loop(Looper.java:123) 12-26 23:40:00.628:E /数据库(30879):在android.app.ActivityThread.main(ActivityThread.java:4627) 12-26 23:40:00.628:E /数据库(30879):在java.lang.reflect.Method.invokeNative(本机方法) 12-26 23:40:00.628:E /数据库(30879):在java.lang.reflect.Method.invoke(Method.java:521) 12-26 23:40:00.628: E/Database(30879): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-26 23:40:00.628:E /数据库(30879):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-26 23:40:00.628:E /数据库(30879):在dalvik.system.NativeStart.main(本机方法) 12-26 23:40:00.628: W/System.err(30879): java.lang.NullPointerException 12-26 23:40:00.639: W/System.err(30879): 在 com.my.examples.SpinnerLoad.fillData(SpinnerLoad.java:59) 12-26 23:40:00.639: W/System.err(30879): 在 com.my.examples.SpinnerLoad.onClick(SpinnerLoad.java:89) 12-26 23:40:00.639: W/System.err(30879): 在 android.view.View.performClick(View.java:2408) 12-26 23:40:00.639: W/System.err(30879): 在 android.view.View$PerformClick.run(View.java:8816) 12-26 23:40:00.639: W/System.err(30879): 在 android.os.Handler.handleCallback(Handler.java:587) 12-26 23:40:00.639: W/System.err(30879): 在 android.os.Handler.dispatchMessage(Handler.java:92) 12-26 23:40:00.639: W/System.err(30879): 在 android.os.Looper.loop(Looper.java:123) 12-26 23:40:00.639: W/System.err(30879): 在 android.app.ActivityThread.main(ActivityThread.java:4627) 12-26 23:40:00.639: W/System.err(30879): 在 java.lang.reflect.Method.invokeNative(本机方法) 12-26 23:40:00.649: W/System.err(30879): 在 java.lang.reflect.Method.invoke(Method.java:521) 12-26 23:40:00.649: W/System.err(30879): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-26 23:40:00.649: W/System.err(30879): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-26 23:40:00.649:W/System.err(30879):在dalvik.system.NativeStart.main(本机方法)
有了日志猫,我可以做一些初学者新手的事情,哈哈..但我已经把它与原来的并排看了几次,不确定我可能搞砸了什么。希望有人能为我阐明这一点,如果需要更多信息,请告诉我。谢谢
I've tried out a couple different ways of loading the spinner from SQLite database, this method seems to be the simplest and easiest. The only thing is since i have added in a couple of my own variables (three more). When I run the edited version to fit my testing needs everything runs correctly except the spinner doesn't fill with data from the database
SpinnerLoad Class
public class SpinnerLoad extends Activity implements OnClickListener {
private Db thisTestDBAdapter;
Button save;
EditText one, two, three;
Spinner spinner;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerform);
save = (Button) findViewById(R.id.spinLoad_save);
save.setOnClickListener(this);
one = (EditText) findViewById(R.id.spinLoad_serial);
two = (EditText) findViewById(R.id.spinLoad_name);
three = (EditText) findViewById(R.id.spinLoad_place);
fillData();
spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
try {
spinner.setOnItemSelectedListener(new Person());
} catch (Exception e) {
e.printStackTrace();
}
}
public static class Person implements OnItemSelectedListener {
static String personReturn;
static boolean personTest = false;
@Override
public void onItemSelected(AdapterView<?> x, View y, int z, long w) {
personReturn = (x.getItemAtPosition(z)).toString();
personTest = true;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
personTest = false;
}
}
private void fillData() {
try {
Cursor coloursCursor;
spinner = (Spinner) findViewById(R.id.spinLoad_spinner);
coloursCursor = thisTestDBAdapter.fetchAllColours();
startManagingCursor(coloursCursor);
String[] from = new String[] { thisTestDBAdapter.KEY_TWO };
int[] to = new int[] { R.id.tvDBViewRow };
SimpleCursorAdapter colourAdapter = new SimpleCursorAdapter(this,
R.layout.db_view_row, coloursCursor, from, to);
spinner.setAdapter(colourAdapter);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.spinLoad_save:
String bx = two.getText().toString();
/**/
String ax = one.getText().toString();
String cx = three.getText().toString();
String returned = Person.personReturn;
Db entry = new Db(this);
entry.open();
entry.createEntry(ax, bx, cx, returned);
entry.close();
fillData();
/**/
break;
}
}
}
Db SQL helper class
public class Db {
private static final String TAG = "SpinnerDBHelper";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
/**/
private static final String KEY_DB = "DataBaseName";
private static final String KEY_TABLE = "DbTable";
public static final String KEY_ROWID = "_rowid"; // 0
public static final String KEY_ONE = "serial";
public static final String KEY_TWO = "name";
public static final String KEY_THREE = "place";
public static final String KEY_FOUR = "returned";
/**/
private static final String DATABASE_CREATE = "CREATE TABLE DbTable (_rowid INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "serial TEXT NOT NULL, "+ "name TEXT NOT NULL, " + "place TEXT NOT NULL, " + "returned TEXT NOT NULL);";
public Db(Context ctx) {
this.mCtx = ctx;
}
public Db open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createEntry(String ax, String bx, String cx, String returned) {
if (mDb == null) {
this.open();
}
ContentValues cv = new ContentValues();
cv.put(KEY_ONE, ax);
cv.put(KEY_TWO, bx);
cv.put(KEY_THREE, cx);
cv.put(KEY_FOUR, returned);
return mDb.insert(KEY_TABLE, null, cv);
}
public boolean deleteEntry(long rowId) {
return mDb.delete(KEY_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public boolean deleteAll() {
if (mDb == null) {
this.open();
}
return mDb.delete(KEY_TABLE, null, null) > 0;
}
public Cursor fetchAllColours() {
if (mDb == null) {
this.open();
}
return mDb.query(KEY_TABLE, new String[] { KEY_ROWID, KEY_TWO},
null, null, null, null, null);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, KEY_DB, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + KEY_TABLE);
onCreate(db);
}
}
}
LOGCAT
12-26 23:39:33.409: W/System.err(30879): java.lang.NullPointerException 12-26 23:39:33.409: W/System.err(30879): at com.my.examples.SpinnerLoad.fillData(SpinnerLoad.java:59) 12-26 23:39:33.409: W/System.err(30879): at com.my.examples.SpinnerLoad.onCreate(SpinnerLoad.java:30) 12-26 23:39:33.418: W/System.err(30879): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 12-26 23:39:33.418: W/System.err(30879): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 12-26 23:39:33.418: W/System.err(30879): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 12-26 23:39:33.418: W/System.err(30879): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 12-26 23:39:33.418: W/System.err(30879): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 12-26 23:39:33.418: W/System.err(30879): at android.os.Handler.dispatchMessage(Handler.java:99) 12-26 23:39:33.418: W/System.err(30879): at android.os.Looper.loop(Looper.java:123) 12-26 23:39:33.418: W/System.err(30879): at android.app.ActivityThread.main(ActivityThread.java:4627) 12-26 23:39:33.428: W/System.err(30879): at java.lang.reflect.Method.invokeNative(Native Method) 12-26 23:39:33.428: W/System.err(30879): at java.lang.reflect.Method.invoke(Method.java:521) 12-26 23:39:33.428: W/System.err(30879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-26 23:39:33.428: W/System.err(30879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-26 23:39:33.428: W/System.err(30879): at dalvik.system.NativeStart.main(Native Method) 12-26 23:40:00.628: E/Database(30879): Error inserting place=zyxwvutsr returned=null serial=123456789 name=abcdefghij 12-26 23:40:00.628: E/Database(30879): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 12-26 23:40:00.628: E/Database(30879): at android.database.sqlite.SQLiteStatement.native_execute(Native Method) 12-26 23:40:00.628: E/Database(30879): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55) 12-26 23:40:00.628: E/Database(30879): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549) 12-26 23:40:00.628: E/Database(30879): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410) 12-26 23:40:00.628: E/Database(30879): at com.my.examples.Db.createEntry(Db.java:56) 12-26 23:40:00.628: E/Database(30879): at com.my.examples.SpinnerLoad.onClick(SpinnerLoad.java:86) 12-26 23:40:00.628: E/Database(30879): at android.view.View.performClick(View.java:2408) 12-26 23:40:00.628: E/Database(30879): at android.view.View$PerformClick.run(View.java:8816) 12-26 23:40:00.628: E/Database(30879): at android.os.Handler.handleCallback(Handler.java:587) 12-26 23:40:00.628: E/Database(30879): at android.os.Handler.dispatchMessage(Handler.java:92) 12-26 23:40:00.628: E/Database(30879): at android.os.Looper.loop(Looper.java:123) 12-26 23:40:00.628: E/Database(30879): at android.app.ActivityThread.main(ActivityThread.java:4627) 12-26 23:40:00.628: E/Database(30879): at java.lang.reflect.Method.invokeNative(Native Method) 12-26 23:40:00.628: E/Database(30879): at java.lang.reflect.Method.invoke(Method.java:521) 12-26 23:40:00.628: E/Database(30879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-26 23:40:00.628: E/Database(30879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-26 23:40:00.628: E/Database(30879): at dalvik.system.NativeStart.main(Native Method) 12-26 23:40:00.628: W/System.err(30879): java.lang.NullPointerException 12-26 23:40:00.639: W/System.err(30879): at com.my.examples.SpinnerLoad.fillData(SpinnerLoad.java:59) 12-26 23:40:00.639: W/System.err(30879): at com.my.examples.SpinnerLoad.onClick(SpinnerLoad.java:89) 12-26 23:40:00.639: W/System.err(30879): at android.view.View.performClick(View.java:2408) 12-26 23:40:00.639: W/System.err(30879): at android.view.View$PerformClick.run(View.java:8816) 12-26 23:40:00.639: W/System.err(30879): at android.os.Handler.handleCallback(Handler.java:587) 12-26 23:40:00.639: W/System.err(30879): at android.os.Handler.dispatchMessage(Handler.java:92) 12-26 23:40:00.639: W/System.err(30879): at android.os.Looper.loop(Looper.java:123) 12-26 23:40:00.639: W/System.err(30879): at android.app.ActivityThread.main(ActivityThread.java:4627) 12-26 23:40:00.639: W/System.err(30879): at java.lang.reflect.Method.invokeNative(Native Method) 12-26 23:40:00.649: W/System.err(30879): at java.lang.reflect.Method.invoke(Method.java:521) 12-26 23:40:00.649: W/System.err(30879): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 12-26 23:40:00.649: W/System.err(30879): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 12-26 23:40:00.649: W/System.err(30879): at dalvik.system.NativeStart.main(Native Method)
With the log cat, I can do just about the beginner Newbie stuff lol .. but I have looked it over a couple times and side by side with the original and not sure what i might have messed up. Hoping someone might be able to shed little light on this for me, if any more info is needed please let me know. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两次引用Spinner
为什么您在这里
和这里
Why you are referencing Spinner two times
here
and here