gpt4 book ai didi

java - setMultiChoiceItems 的光标未填充列表

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:35 24 4
gpt4 key购买 nike

有人可以帮助我使用带有光标的 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) 

最佳答案

我假设当您第一次运行代码时,您会遇到colum_id 异常。 列“_id”不存在,应用程序将会崩溃。这意味着您必须创建像 _columnId 这样的主键。请参阅开头的下划线。您可以在此处阅读更多有关原因的信息。但现在,请记住 sqllite 需要在主键列的开头添加 _About "_id" field in Android SQLite

完成此更改后,接下来您将面临 getCursor() 方法中的 select 查询 问题。有时 * 不起作用,因为它需要列的显式名称,因此将查询更改为从“+TABLE_NAME”中选择“+COLUMN_ID +”、“+COLUMN_ALLERGY +”、“+COLUMN_SELECTED +”

我在下面发布了这些更改,我没有调用任何其他方法,因为我想给您一个开始,您不会在应用程序中看到崩溃。

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 方法。一旦完成,条目就会出现在对话框中。如果您遇到任何问题,请告诉我。

关于java - setMultiChoiceItems 的光标未填充列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52012884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com