android - arraylist 没有正确更新

标签 android arraylist sms sqlite

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

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;
  }
}

以上代码将收件箱中的当前短信保存到数据库中。下面的代码用于在新短信到达时更新您的收件箱。它会对到达的消息进行烘烤,但不会将它们插入数据库。

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;
    }

最佳答案

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

您应该使用准备好的语句来解决这个问题。

查看此 SO Answer举个例子。

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

关于android - arraylist 没有正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11273718/

相关文章:

java - 如何将 HashMap 键转换为列表?

grails - Grails和SMS插件

linux - 在 asterisk sip 上接收短信

java - 每 x 秒检查一次 html 页面

java - MainActivity 中带有 saveText 的 Android Java NullPointerException

android - 如何将存储在数据库中的 Listview 中的值获取到另一个 Activity ?使用Android和Mysql

java - 数据在数组列表中自动存储多次

java - 我如何通过我的存储库获取我的 ID?

java - ArrayList 容量大小增加奇怪的行为

android - 获取用户SIM卡列表并选中-flutter