gpt4 book ai didi

java - 安卓 : How to retrieve specific column in spinner from Sqlite

转载 作者:行者123 更新时间:2023-12-01 13:47:41 26 4
gpt4 key购买 nike

我在 Android 应用程序中使用 SQLite 数据库。我只想在微调器中检索一列。当我运行该应用程序时,该应用程序崩溃了。我的日志猫显示:

Bad request for field slot 0,1. numRows = 1, numColumns = 1

下面是从数据库检索数据并将其放入微调器(NewleadActivity.class)的代码。

这是我的适配器类

public class DbHelper extends SQLiteOpenHelper
{
// Database Version
@SuppressWarnings("unused")
private static final int DATABASE_VERSION = 1;

// Database Name
static String DATABASE_NAME="define_products_database";

// Labels table name
public static final String TABLE_DEFINE_PRODUCT_NAME="define_products";

// Labels Table Columns names
public static final String KEY_PRODUCTS_NAME="product_name";
public static final String KEY_PRODUCTS_DETAILS="products_details";
public static final String KEY_ID="id";

public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+TABLE_DEFINE_PRODUCT_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_PRODUCTS_NAME+" TEXT, "+KEY_PRODUCTS_DETAILS+" TEXT)";
db.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_DEFINE_PRODUCT_NAME);
onCreate(db);

}


/**
* Getting all labels
* returns list of labels
* */

public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();

// Select All Query
String selectQuery = " SELECT " + KEY_PRODUCTS_NAME + " FROM "+ TABLE_DEFINE_PRODUCT_NAME;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
db.close();

// returning lables
return labels;
}

}

这是 Spiner 中包含的 Activity 代码

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.new_lead_activity5);
spinner_Products=(Spinner)findViewById(R.id.products_Spinner);

loadSpinnerData();

}



private void loadSpinnerData() {
// database handler
DbHelper db = new DbHelper(getApplicationContext());

// Spinner Drop down elements
List<String> lables = db.getAllLabels();

// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);

// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinner_Products.setAdapter(dataAdapter);
}

}

这是我的日志猫。

11-27 11:28:28.548: E/AndroidRuntime(2150): FATAL EXCEPTION: main
11-27 11:28:28.548: E/AndroidRuntime(2150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lead_management_project/com.lead_management_project.New_Lead_Activity5}: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.os.Looper.loop(Looper.java:123)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-27 11:28:28.548: E/AndroidRuntime(2150): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 11:28:28.548: E/AndroidRuntime(2150): at java.lang.reflect.Method.invoke(Method.java:507)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-27 11:28:28.548: E/AndroidRuntime(2150): at dalvik.system.NativeStart.main(Native Method)
11-27 11:28:28.548: E/AndroidRuntime(2150): Caused by: java.lang.IllegalStateException: get field slot from row 0 col 1 failed
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.database.CursorWindow.getString_native(Native Method)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.database.CursorWindow.getString(CursorWindow.java:329)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.db_mgmt.DbHelper.getAllLabels(DbHelper.java:65)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.lead_management_project.New_Lead_Activity5.loadSpinnerData(New_Lead_Activity5.java:123)
11-27 11:28:28.548: E/AndroidRuntime(2150): at com.lead_management_project.New_Lead_Activity5.onCreate(New_Lead_Activity5.java:80)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-27 11:28:28.548: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-27 11:28:28.548: E/AndroidRuntime(2150): ... 11 more

最佳答案

伙计,你只查询语句中的单个列并请求第二个列,只需使用下面的方法,我只是将索引从 1 更改为零。

   public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();

// Select All Query
String selectQuery = " SELECT " + KEY_PRODUCTS_NAME + " FROM "+ TABLE_DEFINE_PRODUCT_NAME;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(0));
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
db.close();

// returning lables
return labels;
}

关于java - 安卓 : How to retrieve specific column in spinner from Sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20235287/

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