So in the app I am developing the user can types in a string and selects an address. When they select done the address is put into a geofence and when they enter that geofence a notification is posted. The problem I am having is when I try to add the strings of a row from that name on notification press. The names are the same yet the cursor can not find the row with the string. Thanks in advance!
Here is the error:
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)
Here is the method to get the String[] of values:
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;
}
Here is the code to add the notification and get details:
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());
}
}
"Index -1 requested" indicates that you are querying a Cursor without first calling cursor.moveToFirst()
. Also, it is highly recommended that you use constants with semantic names for your columns, rather than 1, 2 and 3.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.