有人可以协助我使用带有光标的setMultiChoiceItems填充我的列表吗?我的AlertDialog弹出,带有标题和按钮,但列表中没有任何内容。我已经确认数据库中至少有一项,它应该显示在列表中,但目前没有。我认为这与我的光标有关。谢谢。
String isCheckedColumn;
String labelColumn;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new MyDBHandler(this);
// CHECK INSERTION TO DATABASE WORKS AND PRINT TO LOGCAT
mydb.insertAllergy("Nuts", "0");
ArrayList<String> s = mydb.getAllAllergies();
System.out.println(s.get(0));
// INITIALIZE VARIABLES FOR LIST POPUP
isCheckedColumn = "selected";
labelColumn = "allergy";
}
/** LIST SELECTION POPUP */
public void selectAllergens() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Allergens");
builder.setMultiChoiceItems(cursor, isCheckedColumn, labelColumn, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedItemId, boolean isSelected) {
if(isSelected) {
System.out.println("onClick if");
} else {
System.out.println("onClick else");
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
Dialog dialog = builder.create();
dialog.show();
}
这是我的数据库帮助程序类MyDBHandler。我想也许这里应该有一个getCursor方法,所以当我需要初始化一个游标时,我可以这样做。没用。
public class MyDBHandler extends SQLiteOpenHelper {
// DATABASE INFORMATION
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "AllergiesDB.db";
private static final String TABLE_NAME = "allergies_list";
private static final String COLUMN_ID = "allergy_id";
private static final String COLUMN_ALLERGY = "allergy";
private static final String COLUMN_SELECTED = "selected";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY, " +
COLUMN_ALLERGY + " TEXT, " + COLUMN_SELECTED + " TEXT)";
// INITIALIZE DATABASE
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertAllergy(String allergy, String isChecked) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_ALLERGY, allergy);
contentValues.put(COLUMN_SELECTED, isChecked);
db.insert(TABLE_NAME, null, contentValues);
return true;
}
public Cursor getData(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where " + COLUMN_ID + "=" + id + "", null);
return cursor;
}
public boolean updateAllergies(Integer id, String allergy, String isChecked) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_ALLERGY, allergy);
contentValues.put(COLUMN_SELECTED, isChecked);
db.update(TABLE_NAME, contentValues, "id = ? ", new String[] {Integer.toString(id)});
return true;
}
public Integer deleteAllergy(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, "id = ? ", new String[] {Integer.toString(id)});
}
public ArrayList<String> getAllAllergies() {
ArrayList<String> array_list = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
cursor.moveToFirst();
while(cursor.isAfterLast() == false) {
array_list.add(cursor.getString(cursor.getColumnIndex(COLUMN_ALLERGY)));
cursor.moveToNext();
}
return array_list;
}
public ArrayList<String> getSelectedAllergies() {
ArrayList<String> array_list = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
cursor.moveToFirst();
while(cursor.isAfterLast() == false) {
array_list.add(cursor.getString(cursor.getColumnIndex(COLUMN_SELECTED)));
cursor.moveToNext();
}
return array_list;
}
public Cursor getCursor() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLE_NAME, null);
cursor.moveToFirst();
return cursor;
}
}
这是弹出窗口的外观: AlertDialog Popup
在使用“ cursor = mydb.getCursor();”时出现错误。单击打开AlertDialog的按钮会产生错误。游标初始化位于MainActivity的onCreate方法中,因此初始化不会导致崩溃。原因是在setMultiChoiceItems中使用了游标,这使我相信游标没有正确初始化。
--------- beginning of crash
08-24 20:53:50.330 11277-11277/com.healthydreams.lpa.allergyscanner E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.healthydreams.lpa.allergyscanner, PID: 11277
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist. Available columns: [allergy_id, allergy, selected]
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:340)
at android.widget.CursorAdapter.init(CursorAdapter.java:180)
at android.widget.CursorAdapter.<init>(CursorAdapter.java:144)
at android.support.v7.app.AlertController$AlertParams$2.<init>(AlertController.java:1009)
at android.support.v7.app.AlertController$AlertParams.createListView(AlertController.java:1009)
at android.support.v7.app.AlertController$AlertParams.apply(AlertController.java:965)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:982)
at com.healthydreams.lpa.allergyscanner.MainActivity.selectAllergens(MainActivity.java:148)
at com.healthydreams.lpa.allergyscanner.MainActivity.openProfiles(MainActivity.java:85)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
答案 0 :(得分:0)
我认为,当您第一次运行代码时,您会遇到colum_id异常。 column '_id' does not exist
,应用程序将崩溃。这意味着您必须像这样_columnId
创建主键。首先请看下划线。您可以在此处详细了解为什么。但是现在,请记住sqllite在主键列的开头需要_
。
About "_id" field in Android SQLite
完成更改后,接下来您将在select query
方法中遇到getCursor()
的问题。有时*
无法使用,因为它需要列的显式名称,因此请将查询更改为
select " + COLUMN_ID + "," + COLUMN_ALLERGY + "," + COLUMN_SELECTED + " from " + TABLE_NAME
我在下面发布了这些更改,我没有调用任何其他方法,因为我想为您提供一个在应用程序中看不到崩溃的开始。
private static final String COLUMN_ID = " _columnId";
private static final String COLUMN_ALLERGY = "allergy";
private static final String COLUMN_SELECTED = "selected";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + COLUMN_ID " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_ALLERGY + ", " + COLUMN_SELECTED + ")";
......
public Cursor getCursor() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select " + COLUMN_ID + "," +
COLUMN_ALLERGY + "," + COLUMN_SELECTED + " from " + TABLE_NAME,
null);
cursor.moveToFirst();
return cursor;
}
您可以调用将在数据库中填充数据的方法,然后调用getCursor
方法。一旦有了,条目将出现在对话框中。
让我知道您是否遇到任何问题。