2012-06-30 137 views
2

我正在使用contentobserver來監控SMS。它一切正常。當我嘗試將這些短信保存到數據庫時,它顯示特定SMS的錯誤error near "t" syntax error。當我刪除這個特定的SMS時,沒有問題。安裝後,它會按順序正確顯示所有消息。但是錯誤發送到我的數組列表的末尾。此後,從我的手機發送的短信在列表之間更新,而不是最後一個位置。請幫忙。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; 
    } 
} 

上面的代碼保存在收件箱中,以數據庫的當前短信。下面的代碼用於在新短信到達時更新您的收件箱。它爲到達的消息做烤麪包,但不會將它們插入到數據庫中。

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

猜測在一個短信中有某種受限制的字符/字。

您應該使用預處理語句來處理該問題。

查看此SO Answer爲例。

對於第二個關於顯示順序的問題,在查詢中更改/使用ORDER BY來設置正確的順序。