我编写了一个小程序,我接受用户的数据并将数据存储到数据库中。要接受用户的数据,我在DialogBox中使用EditText。
现在我正在尝试从数据库中获取数据,并且需要将数据显示在DialogBox中,在创建对话框中.....
UploadActivity.java:
@Override
public Dialog onCreateDialog(int id) {
helper = new DBHelper(this);
AlertDialog dialogDetails = null;
switch (id) {
case DIALOG_LOGIN:
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.dialog_layout, null);
AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
dialogbuilder.setTitle("Image Information");
dialogbuilder.setView(dialogview);
dialogDetails = dialogbuilder.create();
break;
}
return dialogDetails;
}
@Override
public void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_LOGIN:
final AlertDialog alertDialog = (AlertDialog) dialog;
Button uploadButton = (Button) alertDialog
.findViewById(R.id.btnUploadData);
Button saveButton = (Button) alertDialog
.findViewById(R.id.btnSaveXML);
Button cancelButton = (Button) alertDialog
.findViewById(R.id.btnCancelDialog);
editImageName = (EditText)alertDialog
.findViewById(R.id.editImageName);
editPersonName = (EditText) alertDialog
.findViewById(R.id.editPersonName);
editPersonaEmail = (EditText) alertDialog
.findViewById(R.id.editPersonEmail);
editPersonTelephone = (EditText) alertDialog
.findViewById(R.id.editPersonTelephone);
editImageName.setText(fileName);
fetchData();
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(DBHelper.IMAGE_NAME, editImageName.getText().toString());
values.put(DBHelper.PERSON_NAME, editPersonName.getText().toString());
values.put(DBHelper.PERSON_EMAIL, editPersonaEmail.getText().toString());
values.put(DBHelper.PERSON_PHONE, editPersonTelephone.getText().toString());
// Call insert method of SQLiteDatabase Class and close after
// performing task
db = helper.getWritableDatabase();
db.insert(DBHelper.TABLE_NAME, null, values);
db.close();
Toast.makeText(UploadActivity.this, "Stored to Database",
Toast.LENGTH_LONG).show();
}
});
cancelButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
}
}
// Fetch Fresh data from database and display into listview
@SuppressWarnings("deprecation")
private void fetchData() {
db = helper.getReadableDatabase();
Cursor c = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.dialog_layout,
c,
new String[] { DBHelper.IMAGE_NAME, DBHelper.PERSON_NAME,
DBHelper.PERSON_EMAIL, DBHelper.PERSON_PHONE },
new int[] { R.id.editImageName, R.id.editPersonName, R.id.editPersonEmail, R.id.editPersonTelephone });
}
DBHelper.java:
public class DBHelper extends SQLiteOpenHelper {
// Static Final Variable database meta information
static final String DATABASE_NAME = "MyDatabase.db";
static final int VERSION = 1;
static final String TABLE_NAME = "MyTable";
static final String IMAGE_CODE = "imageCode";
static final String IMAGE_NAME = "imageName";
static final String PERSON_NAME = "personName";
static final String PERSON_EMAIL = "personEmail";
static final String PERSON_PHONE = "personPhone";
// Override constructor
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
// Override onCreate method
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE " + TABLE_NAME +
" ( " + IMAGE_CODE + " INTEGER PRIMARY KEY AUTOINCREMENT, " // Auto Increment
+ IMAGE_NAME + " text, " // Image Name
+ PERSON_NAME + " text, " // Person Name
+ PERSON_EMAIL + " text, " // Person Email
+ PERSON_PHONE + " text )"); // Person Phone
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop old version table
db.execSQL("Drop table " + TABLE_NAME);
// Create New Version table
onCreate(db);
}
}
我通过点击列表中的数据按钮来调用Dialog ...
// Data
final Button btnData = (Button) convertView.findViewById(R.id.btnData);
btnData.setTextColor(Color.BLACK);
btnData.setOnClickListener(new View.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(View v) {
fileName=ImageList.get(position).toString().substring(strPath.lastIndexOf('/')+1, strPath.length());
showDialog(DIALOG_LOGIN);
}
});
日志报告: -
11-23 05:19:57.953: W/dalvikvm(1475): threadid=1: thread exiting with uncaught exception (group=0x41465700)
11-23 05:19:57.984: E/AndroidRuntime(1475): FATAL EXCEPTION: main
11-23 05:19:57.984: E/AndroidRuntime(1475): java.lang.IllegalArgumentException: column '_id' does not exist
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.widget.CursorAdapter.init(CursorAdapter.java:168)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.widget.CursorAdapter.<init>(CursorAdapter.java:116)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
11-23 05:19:57.984: E/AndroidRuntime(1475): at com.example.db.UploadActivity.fetchData(UploadActivity.java:416)
11-23 05:19:57.984: E/AndroidRuntime(1475): at com.example.db.UploadActivity.onPrepareDialog(UploadActivity.java:284)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.app.Activity.onPrepareDialog(Activity.java:3027)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.app.Activity.showDialog(Activity.java:3090)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.app.Activity.showDialog(Activity.java:3041)
11-23 05:19:57.984: E/AndroidRuntime(1475): at com.example.db.UploadActivity$ImageAdapter$2.onClick(UploadActivity.java:222)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.view.View.performClick(View.java:4240)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.view.View$PerformClick.run(View.java:17721)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.os.Handler.handleCallback(Handler.java:730)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.os.Looper.loop(Looper.java:137)
11-23 05:19:57.984: E/AndroidRuntime(1475): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-23 05:19:57.984: E/AndroidRuntime(1475): at java.lang.reflect.Method.invokeNative(Native Method)
11-23 05:19:57.984: E/AndroidRuntime(1475): at java.lang.reflect.Method.invoke(Method.java:525)
11-23 05:19:57.984: E/AndroidRuntime(1475): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-23 05:19:57.984: E/AndroidRuntime(1475): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-23 05:19:57.984: E/AndroidRuntime(1475): at dalvik.system.NativeStart.main(Native Method)
第416行是:
new int[] { R.id.editImageName, R.id.editPersonName, R.id.editPersonEmail, R.id.editPersonTelephone });
第284行是:
fetchData();
第222行是:
showDialog(DIALOG_LOGIN);
答案 0 :(得分:1)
如果您从此代码db = helper.getReadableDatabase();
收到错误,则表示您尚未初始化helper
,并且在初始化之后以及插入get java.lang.IllegalArgumentException: column '_id' does not exist
之类的错误时因为您正在尝试使用需要名为_id的列的游标。它就像编辑表创建语句和添加名为_id的列一样简单。
它的声明看起来像这样:
_id INTEGER PRIMARY KEY AUTOINCREMENT
添加此项,然后您就可以使用它了。我认为这是使用SimpleCursorAdapter所必需的。
"CREATE TABLE IF NOT EXISTS TABLE_NAME( _id INTEGER PRIMARY KEY AUTOINCREMENT, ...);"
在左侧perenthesis和_id
之间添加一个空格答案 1 :(得分:0)
它要么
helper.getReadableDatabase();
返回null
引用或
您尚未初始化
helper