数据库对象未被执行

时间:2015-04-26 06:37:50

标签: android sqlite service

我在服务中运行以下线程。但是,如果我在数据库中有3行或4行数据,则最后一行不会被执行。如果我有4行数据,则只执行3行。谁能告诉我这是什么原因?

 new Thread(new Runnable() {
        @Override
        public void run() {

            List<Greetings>greetings = db.getAllGreetings();
                 if(db.getGreetingsCount()>0) {
                     do {
                         for (Greetings g : greetings) {
                             Calendar cal = Calendar.getInstance();
                             d1 = cal.get(Calendar.DAY_OF_MONTH);
                             d2 = cal.get(Calendar.MONTH);
                             d3 = cal.get(Calendar.YEAR);
                             t1 = cal.get(Calendar.HOUR);
                             t2 = cal.get(Calendar.MINUTE);
                             t3 = cal.get(Calendar.AM_PM);
                             if (t3 == 1) {
                                 t4 = "PM";
                             } else {
                                 t4 = "AM";
                             }
                             StringBuilder strt = new StringBuilder().append(t1).append(" : ").append(t2)
                                     .append(" ").append(t4);

                             tc = strt.toString();
                             StringBuilder builder = new StringBuilder().append(String.valueOf(d3) + "-").append((String.valueOf(d2 + 1)) + "-")
                                     .append(String.valueOf(d1));

                             d = builder.toString();
                             cdt = d + "\t" + tc;
                             //Log.d(cdt, "About to execute MyTask");
                             time = g.getTime();
                             date = g.getDate();
                             phone = g.getPhoneNumber();
                             msg = g.getMessage();
                             id = g.getID();
                             dnt = date + "\t" + time;
                             //Log.d(dnt, "About to execute MyTask");
                             if (dnt.equals(cdt)) {
                                 Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);

                                 aint.putExtra("msg", msg);
                                 aint.putExtra("phone", phone);
                                 aint.putExtra("id", id);
                                 PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
                                 alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
                                 db.deleteGreetings(g);
                             }

                         }
                         k = db.getGreetingsCount();

                     } while ( k !=0 );
                 }

        }
    }).start();
     return START_NOT_STICKY;

getAllgreetings和getGreetingsCount类如下所示

 public List<Greetings> getAllGreetings() {
    List<Greetings> greetingList = new ArrayList<Greetings>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_GREETINGS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Greetings greeting = new Greetings();
            greeting.setID(Integer.parseInt(cursor.getString(0)));
            greeting.setDate(cursor.getString(1));
            greeting.setPhoneNumber(cursor.getString(2));
            greeting.setTime(cursor.getString(3));
            greeting.setMessage(cursor.getString(4));
            // Adding contact to list
            greetingList.add(greeting);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();

    // return greeting list
    return greetingList;
}

public int getGreetingsCount() {
    String countQuery = "SELECT  * FROM " + TABLE_GREETINGS;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int a = cursor.getCount();
    cursor.close();
    db.close();
    // return count
    return a;
}

0 个答案:

没有答案