在通知中随机传递Sqlite数据

时间:2019-05-01 14:58:04

标签: android sqlite push-notification android-sqlite android-notifications

我正在Android应用程序中使用预填充的Sqlite数据库,该数据库具有三个表(id,word,define)。我想做的是从数据库中随机向用户显示通知。我正在执行类似下面的代码的操作,但是它不起作用。

DatabaseHelper.java

public Cursor getNotification(int id) {
    SQLiteDatabase db = this.getWritableDatabase();
    String Query = "Select * from " + TABLE_DICTIONARY + " where id = " + id  ;
    Cursor cursor = db.rawQuery(Query, null);
    if(cursor.getCount() <= 0){
        cursor.close();
    }
    cursor.close();
    return cursor;
}

这是我的MainActivity.java

private void showNotification() {
   int position =  dictionaryAdapter.cursor.getPosition();
  int cursorCount = dictionaryAdapter.cursor.getCount();

    final int random = new Random().nextInt((cursorCount) - 1);

   Cursor i =  mDBHelper.getNotification(random);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this,CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(String.valueOf(i))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
    managerCompat.notify(1,mBuilder.build());

}

1 个答案:

答案 0 :(得分:1)

修复第1部分

简而言之,您可以利用以下内容:-

public Cursor getNotification() {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.query(TABLE_DICTIONARY,null,null,null,null,null,"random()","1");
    return cursor;
}

以上等同于生成SQL:-

SELECT * FROM your_table ORDER BY random() LIMIT 1; 
  • 请注意,您的表只是表名的代表,很可能需要更改以适合您的表名。

您不妨看一下query

当然,您可以在rawQuery中使用上面的SQL。但是,通常建议使用便捷方法,例如 query ,而不是 rawQuery execSQL 方法。使用。

以上方法是通过使用SQLite的 random()函数对行进行排序,然后 LIMIT 将结果行的数量仅为1来实现的。

其他问题

  1. 此外,重要的是,您不能按照以下方式使用封闭的光标:-

    •   

      关闭游标,释放其所有资源并进行调整   完全无效。与deactivate()不同,调用requery()不会   使游标再次有效。

  2. 此外,要从游标访问存储的数据,您必须移至一行,因为最初的游标位于第一行之前(例如,下面使用i.moveToFirst())。

  3. 此外,当返回单个随机行时,无需尝试随机检索其中之一。但是,您应该检查是否已返回行。

修复第2部分

因此,活动中的代码可能是:-

private void showNotification() {


    Cursor i =  mDBHelper.getNotification();
    if (i.moveToFirst()) {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this,CHANNEL_ID)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(String.valueOf(i)) //???????????
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
        managerCompat.notify(1,mBuilder.build());
    } else {
        //code here to handle nothing being obtained and thus 
    }
    i.close(); // You should always close a Cursor when done with it.
}

另一个问题

使用String.valueOf(i)不会是数据库中的任何值,而将是Cursor对象的toString方法产生的值。类似于 android.database.sqlite.SQLiteCursor@a40a2ab

修复第3部分

您可能想要一些类似的东西:-

    .setContentTitle(i.getString(i.getColumnIndex("word") + " is defined as " + i.getString(i.getColumnIndex("definition"))) // builds the title from columns of the selected row

这会将标题设置为 the_word定义为the_definition_of_the_word ,其中 the_word 是存储在 word 列中的值,以及 the_definition_of_the_word < / strong>是存储在定义列中的值。

其他

  • 注意:Cursor的getColumnIndex方法存在一个依赖于大小写的错误。因此,建议对列名称使用常量值,因此与使用列名称进行硬编码相比,这种情况始终是正确的。 (例如,您使用常量TABLE_DICTIONARY)。