遍历 android sqlite 数据库
Looping through android sqlite database
我正在尝试将 Whatsapp/Email 通知样式添加到我的应用程序中,当一个通知出现和另一个通知出现时,它们都显示在同一条消息中。我通过将通知保存到数据库来执行此操作,该数据库在单击消息时被删除。目前,我的循环运行良好,除非我发送超过 2 个通知,例如 4 个。
[![工作时][1]][1]
当它坏了..
[![图片][2]][2]
数字代表我发送通知的顺序。如您所见,Four 重复了两次而不是 none..屏幕截图 2 从上到下所需的顺序应该是 Four->Three->Two->One.
这是循环的代码..
Cursor cur;
............
............
int imsg = cur.getColumnIndex(KEY_MSG);
int ititle = cur.getColumnIndex(KEY_TITLE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));
if (c == 0) {
//when DB is empty...
inboxStyle.addLine(msg);
} else {
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
num++;
inboxStyle.setBigContentTitle("Reelforge");
inboxStyle.addLine(makeNotificationLine(title, msg));
inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
}
}
inboxStyle.setSummaryText(num + " new notifications");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
.setContentText(msg);
mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
mBuilder.setAutoCancel(true);
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(contentIntent);
the_db.close();
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
String x = m + "";
SimpleDateFormat sdf = new SimpleDateFormat("LLL d, yyyy");
sdf.setTimeZone(TimeZone.getDefault());
String date = sdf.format(new Date());
try {
the_db.open();
the_db.createmsgEntry(x, title, msg);
the_db.createmsgEntry2(x, title, msg, date);
the_db.close();
} catch (Exception e) {
the_db.close();
} finally {
if (the_db != null) {
the_db.close();
}
}
mNotificationManager.notify(N_ID, mBuilder.build());
public long createmsgEntry(String s2, String s3, String s4) {
ContentValues cv = new ContentValues();
cv.put(KEY_MSGID, s2);
cv.put(KEY_TITLE, s3);
cv.put(KEY_MSG, s4);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
如果你在这里查看文档 Applying an expanded layout to a notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.../
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// Set up inboxStyle
inboxStyle.setBigContentTitle(.../
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.
这些行是在创建生成器之后添加的。
首先:照顾好你的数据库;
// To do.. add new notifications to db, parse values etc,
然后创建新的构建器并循环遍历您的数据库以获取要添加到行的值。
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
..../
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));
if (c == 0) {
//when DB is empty...
inboxStyle.addLine(msg);
} else {
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
num++;
inboxStyle.setBigContentTitle("Reelforge");
inboxStyle.addLine(makeNotificationLine(title, msg));
inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
}
}
inboxStyle.setSummaryText(num + " new notifications");
mNotificationManager.notify(N_ID, mBuilder.build());
每次像这样更新通知时,您实际上需要创建一个新的通知生成器。您不能向现有通知构建器添加行。你可以更新一些东西,比如标题。
一些不错的链接。
Update android notification with new line is removing previous lines
终于解决了....
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int num = 0;
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
String x = m + "";
SimpleDateFormat sdf = new SimpleDateFormat("LLL d, yyyy");
sdf.setTimeZone(TimeZone.getDefault());
String date = sdf.format(new Date());
Intent i = new Intent(this, MainActivity.class);
i.setAction("gcm");
//opened after clicking
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);
try {
the_db.open();
c = the_db.getCount();
cur = the_db.getmsgData();
the_db.createmsgEntry(x, title, msg);
the_db.createmsgEntry2(x, title, msg, date);
} catch (Exception e) {
the_db.close();
}
int imsg = cur.getColumnIndex(KEY_MSG);
int ititle = cur.getColumnIndex(KEY_TITLE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));
if (c == 0) {
num++;
inboxStyle.addLine(msg);
} else {
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
num++;
inboxStyle.setBigContentTitle("New Notifications");
inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
}
}
inboxStyle.setSummaryText(num + " new notifications");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
.setContentText(msg);
mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
mBuilder.setAutoCancel(true);
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(contentIntent);
the_db.close();
mNotificationManager.notify(N_ID, mBuilder.build());
我正在尝试将 Whatsapp/Email 通知样式添加到我的应用程序中,当一个通知出现和另一个通知出现时,它们都显示在同一条消息中。我通过将通知保存到数据库来执行此操作,该数据库在单击消息时被删除。目前,我的循环运行良好,除非我发送超过 2 个通知,例如 4 个。 [![工作时][1]][1]
当它坏了..
[![图片][2]][2]
数字代表我发送通知的顺序。如您所见,Four 重复了两次而不是 none..屏幕截图 2 从上到下所需的顺序应该是 Four->Three->Two->One.
这是循环的代码..
Cursor cur;
............
............
int imsg = cur.getColumnIndex(KEY_MSG);
int ititle = cur.getColumnIndex(KEY_TITLE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));
if (c == 0) {
//when DB is empty...
inboxStyle.addLine(msg);
} else {
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
num++;
inboxStyle.setBigContentTitle("Reelforge");
inboxStyle.addLine(makeNotificationLine(title, msg));
inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
}
}
inboxStyle.setSummaryText(num + " new notifications");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
.setContentText(msg);
mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
mBuilder.setAutoCancel(true);
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(contentIntent);
the_db.close();
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
String x = m + "";
SimpleDateFormat sdf = new SimpleDateFormat("LLL d, yyyy");
sdf.setTimeZone(TimeZone.getDefault());
String date = sdf.format(new Date());
try {
the_db.open();
the_db.createmsgEntry(x, title, msg);
the_db.createmsgEntry2(x, title, msg, date);
the_db.close();
} catch (Exception e) {
the_db.close();
} finally {
if (the_db != null) {
the_db.close();
}
}
mNotificationManager.notify(N_ID, mBuilder.build());
public long createmsgEntry(String s2, String s3, String s4) {
ContentValues cv = new ContentValues();
cv.put(KEY_MSGID, s2);
cv.put(KEY_TITLE, s3);
cv.put(KEY_MSG, s4);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
如果你在这里查看文档 Applying an expanded layout to a notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.../
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
// Set up inboxStyle
inboxStyle.setBigContentTitle(.../
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.
这些行是在创建生成器之后添加的。 首先:照顾好你的数据库;
// To do.. add new notifications to db, parse values etc,
然后创建新的构建器并循环遍历您的数据库以获取要添加到行的值。
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
..../
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));
if (c == 0) {
//when DB is empty...
inboxStyle.addLine(msg);
} else {
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
num++;
inboxStyle.setBigContentTitle("Reelforge");
inboxStyle.addLine(makeNotificationLine(title, msg));
inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
}
}
inboxStyle.setSummaryText(num + " new notifications");
mNotificationManager.notify(N_ID, mBuilder.build());
每次像这样更新通知时,您实际上需要创建一个新的通知生成器。您不能向现有通知构建器添加行。你可以更新一些东西,比如标题。
一些不错的链接。
Update android notification with new line is removing previous lines
终于解决了....
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int num = 0;
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
String x = m + "";
SimpleDateFormat sdf = new SimpleDateFormat("LLL d, yyyy");
sdf.setTimeZone(TimeZone.getDefault());
String date = sdf.format(new Date());
Intent i = new Intent(this, MainActivity.class);
i.setAction("gcm");
//opened after clicking
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);
try {
the_db.open();
c = the_db.getCount();
cur = the_db.getmsgData();
the_db.createmsgEntry(x, title, msg);
the_db.createmsgEntry2(x, title, msg, date);
} catch (Exception e) {
the_db.close();
}
int imsg = cur.getColumnIndex(KEY_MSG);
int ititle = cur.getColumnIndex(KEY_TITLE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(makeNotificationLine(title, ""));
if (c == 0) {
num++;
inboxStyle.addLine(msg);
} else {
for (cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()) {
num++;
inboxStyle.setBigContentTitle("New Notifications");
inboxStyle.addLine(makeNotificationLine(cur.getString(ititle), cur.getString(imsg)));
}
}
inboxStyle.setSummaryText(num + " new notifications");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
.setContentText(msg);
mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
mBuilder.setAutoCancel(true);
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(contentIntent);
the_db.close();
mNotificationManager.notify(N_ID, mBuilder.build());