I have a problem in my app. I have some lists and each of them has a date. If the date of a list is today I made a Notification,and in my notification I put the items from that list. The problem is that if my list empty I get force Close. I put the condition to make the notification only if the cursor is not null,but it seems that this not works. Can you help me to find where is the problem?
Here is a part of the code:
cr = db.fetchListId(id);
startManagingCursor(cr);
s = new StringBuilder(100);
if (cr != null) {
do {
String myList = "";
myList += cr.getString(2) + " " + cr.getString(3)
+ cr.getString(4) + "," + "\n";
s.append(myList);
} while (cr.moveToNext());
s.append('.');
System.out.println("!!heyeheeye!" + s + "!!!!");
hey(list);
}
where hey(list) is a function where is made the notification.
private void hey(String list) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "Hey";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon,
NotificationTicket, when);
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.proba);
contentView.setImageViewResource(R.id.image, R.drawable.icon);
contentView.setTextViewText(R.id.notif,
"Today,you must go for shopping for your list '" + list + "'. "
+ "Don't forget!!!" + "\n" + "You must buy :" + "\n"
+ s.toString() + "\n");
notification.contentView = contentView;
Context context = getApplicationContext();
Intent notifyIntent = new Intent(context, Lists.class);
Intent notificationIntent = new Intent(this, Lists.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.contentIntent = contentIntent;
notificationManager.notify(ID_NOTIFICATION, notification);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
Cursors are what contain the result set of a query made against a database in Android. The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that were returned from the query as well as iterate over the rows of the result set.
Calling moveToFirst() does two things: it allows you to test whether the query returned an empty set (by testing the return value) and it moves the cursor to the first result (when the set is not empty).
Add the following check to see if the cursor has any results:
if (cr != null && cr.moveToFirst()) {
// ...
}
You need to moveToFirst() before getting data. Because you're using a do...while instead of while(), you're not initialising the cursor properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With