gpt4 book ai didi

java - 如何使用字符串从 SQL 数据库获取行

转载 作者:行者123 更新时间:2023-12-02 04:20:28 40 4
gpt4 key购买 nike

因此,在我正在开发的应用程序中,用户可以输入字符串并选择地址。当他们选择完成时,地址将被放入地理围栏中,当他们进入该地理围栏时,会发布通知。我遇到的问题是当我尝试在通知按下时添加该名称中的一行字符串时。名称相同,但游标找不到包含该字符串的行。提前致谢!

这是错误:

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.nick.mowen.receiptmanager.ManagerDatabaseAdapter.getDataArray(ManagerDatabaseAdapter.java:64)
at com.nick.mowen.receiptmanager.GeofenceTransitionsIntentService.getAppToOpen(GeofenceTransitionsIntentService.java:35)
at com.nick.mowen.receiptmanager.GeofenceTransitionsIntentService.sendNotification(GeofenceTransitionsIntentService.java:82)
at com.nick.mowen.receiptmanager.GeofenceTransitionsIntentService.onHandleIntent(GeofenceTransitionsIntentService.java:64)
at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.os.HandlerThread.run(HandlerThread.java:61)

以下是获取值的 String[] 的方法:

public String[] getDataArray(String name) {
String[] columns = {ManagerHelper.NAME,ManagerHelper.CODE,ManagerHelper.ADDRESS};
SQLiteDatabase db = helper.getWritableDatabase();
Cursor cursor = db.query(ManagerHelper.TABLE_NAME, columns, ManagerHelper.NAME + " = '" + name + "'", null, null, null, null);
String[] data = new String[3];
data[0] = cursor.getString(1);
data[1] = cursor.getString(2);
data[2] = cursor.getString(3);
return data;
}

以下是添加通知并获取详细信息的代码:

public String[] getAppToOpen(String names) {
managerDatabaseAdapter = new ManagerDatabaseAdapter(this);
selectArgs = managerDatabaseAdapter.getDataArray(names);
return selectArgs;
}

protected void onHandleIntent(Intent intent) {
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = "There is hopefully no error";
Log.e(TAG, errorMessage);
return;
}

// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();

// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {

// Get the geofences that were triggered. A single event can trigger
// multiple geofences.
List triggeringGeofences = geofencingEvent.getTriggeringGeofences();

// Get the transition details as a String.
String geofenceTransitionDetails = getGeofenceTransitionDetails(
geofenceTransition,
triggeringGeofences
);

// Send notification and log the transition details.
sendNotification(geofenceTransitionDetails);
//Log.i(TAG, geofenceTransitionDetails);
} else {
// Log the error.
}
}

private String getGeofenceTransitionDetails(int geofenceTransition, List triggeringGeofences) {
String[] split = {":", " "};
String name = triggeringGeofences.get(0).toString();
newSplit = name.split(split[0]);
secondSplit = newSplit[1].split(split[1]);
return secondSplit[0];
}

private void sendNotification(String geofenceTransitionDetails) {
NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this).setContentTitle("Receipt Code Reminder").setContentText(geofenceTransitionDetails).setSmallIcon(R.drawable.ic_stat_maps_local_restaurant).setTicker("Receipt Code Reminder").setAutoCancel(true);
Intent localIntent = new Intent(this, ViewCodeActivity.class);
localIntent.putExtra(EXTRA_MESSAGE, getAppToOpen(geofenceTransitionDetails));
TaskStackBuilder localTaskStackBuilder = TaskStackBuilder.create(this);
localTaskStackBuilder.addParentStack(ViewCodeActivity.class);
localTaskStackBuilder.addNextIntent(localIntent);
builder.setContentIntent(localTaskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).notify(this.mID, builder.build());
}

}

最佳答案

“已请求索引 -1”表示您正在查询 Cursor,而没有先调用 cursor.moveToFirst()。另外,强烈建议您对列使用带有语义名称的常量,而不是 1、2 和 3。

关于java - 如何使用字符串从 SQL 数据库获取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32834634/

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