arraylist未正确更新

时间:2012-06-30 11:30:06

标签: android arraylist sms sqlite

我正在使用contentobserver来监控短信。一切正常。当我尝试将这些SMS保存到数据库时,它会显示特定SMS的错误error near "t" syntax error。当我删除这个特定的短信时没有问题。安装后,它会按顺序正确显示所有消息。但错误被发送到我的arraylist的末尾。此后,我的手机发送的短信也会在列表之间更新,而不是在最后一个位置。请帮忙。

adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,list);
setListAdapter(adapter);
data = Incoming_outgoing_smsActivity.this.openOrCreateDatabase("Messages", MODE_PRIVATE, null);
data.execSQL("CREATE TABLE IF NOT EXISTS recor(text varchar(300));");
Cursor cur = data.rawQuery("SELECT * FROM recor", null);
while(cur.moveToNext()) {
  String content = cur.getString(cur.getColumnIndex("text"));
  backward_list.add(content);
  list.add(content);
}
adapter.notifyDataSetChanged();
Cursor cursor = getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
while(cursor.moveToNext()) {
  String number = cursor.getString(cursor.getColumnIndex("address"));
  String[] projection = new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME};
  Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
  Cursor cursor_name = getContentResolver().query(contactUri, projection, null, null, null);
  String body = cursor.getString(cursor.getColumnIndex("body"));
  String type = cursor.getString(cursor.getColumnIndex("type"));
  long date1= cursor.getLong(cursor.getColumnIndex("date"));
  SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(date1);
  try {
    int n = cursor.getInt(cursor.getColumnIndex("type"));
    switch (n) {
      case 1:
        String message = "FROM "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
        if(backward_list.contains(message)) {
          continue;
        } else {
          list.add(message);
          backward_list.add(message);
          data.execSQL("INSERT INTO recor VALUES('"+message+"')");
        }
        break;
      case 2:
        String messag = "TO "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
        if(backward_list.contains(messag)) {
          continue;
        } else {
          list.add(messag);
          backward_list.add(messag);
          data.execSQL("INSERT INTO recor VALUES('"+messag+"')");
        }
        break;
      default:
        break;
    }
  }
  catch (Exception e) {
    // TODO: handle exception
    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    continue;
  }
}

以上代码将收件箱中的当前SMS保存到数据库中。以下代码用于在新短信到达时更新您的收件箱。它确实为已到达的消息干杯,但不会将它们插入到数据库中。

data = Incoming_outgoing_smsActivity.this.openOrCreateDatabase("Messages", MODE_PRIVATE, null);
data.execSQL("CREATE TABLE IF NOT EXISTS recor(text varchar(300));");
super.onChange(selfChange);
Cursor cursor = getContentResolver().query(Uri.parse("content://sms"), null, null, null, null);
while(cursor.moveToNext()) {
  String number = cursor.getString(cursor.getColumnIndex("address"));
  String[] projection = new String[] {
    ContactsContract.PhoneLookup.DISPLAY_NAME};
    Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    Cursor cursor_name = getContentResolver().query(contactUri, projection, null, null, null);
    if(cursor_name.moveToFirst()) {
      name = cursor_name.getString(cursor_name.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }
    String body = cursor.getString(cursor.getColumnIndex("body"));
    String type = cursor.getString(cursor.getColumnIndex("type"));
    long date1= cursor.getLong(cursor.getColumnIndex("date"));
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss.SSS");
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(date1);
    int n = cursor.getInt(cursor.getColumnIndex("type"));
    switch (n) {
      case 1:
        String message = "FROM "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
        if(backward_list.contains(message)) {
          continue;
        } else {
          list.add(message);
          backward_list.add(message);
          data.execSQL("INSERT INTO recor VALUES('"+message+"')");
        }
        break;
      case 2:
        String messag = "TO "+number+"\n"+formatter.format(calendar.getTime())+"\n"+"Message:-"+body;
        if(backward_list.contains(messag)) {
          continue;
        } else {
          list.add(messag);
          backward_list.add(messag);
          data.execSQL("INSERT INTO recor VALUES('"+messag+"')");
        }
        break;
      default:
        break;
    }

1 个答案:

答案 0 :(得分:1)

猜测一条短信中有某种受限制的字符/单词。

您应该使用准备好的陈述来处理这个问题。

有关示例,请参阅此SO Answer

关于显示顺序的第二个问题,请在查询中更改/使用ORDER BY来设置正确的顺序。