Android中的空游标
我的应用程序有问题。我有一些清单,每个清单都有日期。如果列表的日期是今天,我会发出通知,并在通知中列出列表中的项目。问题是,如果我的列表为空,我会强制关闭。我设置了只在光标不为null时发出通知的条件,但这似乎不起作用。你能帮我找出问题出在哪里吗 以下是代码的一部分:Android中的空游标,android,notifications,Android,Notifications,我的应用程序有问题。我有一些清单,每个清单都有日期。如果列表的日期是今天,我会发出通知,并在通知中列出列表中的项目。问题是,如果我的列表为空,我会强制关闭。我设置了只在光标不为null时发出通知的条件,但这似乎不起作用。你能帮我找出问题出在哪里吗 以下是代码的一部分: cr = db.fetchListId(id); startManagingCursor(cr); s = new StringBuilder(100); if (cr != null) { do { String
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);
}
添加以下检查以查看光标是否有任何结果:
if (cr != null && cr.moveToFirst()) {
// ...
}
在获取数据之前,需要先移动到first()。因为使用的是do…while而不是while(),所以没有正确初始化光标