2013-04-10 54 views
2

我有我的SMSReceiverActivity.java下面的代碼:不能增加價值的ListView

package com.example.smsTest; 

import java.util.ArrayList; 
import java.util.List; 

import android.app.ListActivity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class SMSReceiverActivity extends ListActivity { 
    private SQLiteAdapter mySQLiteAdapter; 

    private List<String> bodyarr = new ArrayList<String>(); 
    private ArrayAdapter<Message> arrayAdpt; 

    private BroadcastReceiver mIntentReceiver; 
    ListView listview; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_smsreceiver); 

     mySQLiteAdapter = new SQLiteAdapter(this); 
     mySQLiteAdapter.openToRead(); 

     List<Message> bodyarr = mySQLiteAdapter.getAllMessages(); 

     listview=this.getListView(); 

     arrayAdpt = new ArrayAdapter<Message>(SMSReceiverActivity.this, 
       android.R.layout.simple_list_item_1, bodyarr); 
     listview.setAdapter(arrayAdpt);  
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN"); 
     mIntentReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       String msg = intent.getStringExtra("get_msg"); 

       //Process the sms format and extract body &amp; phoneNumber 
       msg = msg.replace("\n", ""); 
       String body = msg.substring(msg.lastIndexOf(":")+1, msg.length()); 
       String pNumber = msg.substring(0,msg.lastIndexOf(":")); 

       bodyarr.add(body); 
       arrayAdpt.notifyDataSetChanged(); 

       /* 
       * Create/Open a SQLite database 
       * and fill with dummy content 
       * and close it 
       */ 
       mySQLiteAdapter = new SQLiteAdapter(SMSReceiverActivity.this); 
       mySQLiteAdapter.openToWrite(); 
       //mySQLiteAdapter.deleteAll(); 

       mySQLiteAdapter.insert(body); 

       mySQLiteAdapter.close(); 
      } 
    }; 

    this.registerReceiver(mIntentReceiver, intentFilter); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     this.unregisterReceiver(this.mIntentReceiver); 
    } 

} 

這是SQLiteAdapter.java的代碼:

package com.example.smsTest; 

import java.util.ArrayList; 
import java.util.List; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 

public class SQLiteAdapter { 

    public static final String MYDATABASE_NAME = "MY_DATABASE"; 
    public static final String MYDATABASE_TABLE = "MY_TABLE"; 
    public static final int MYDATABASE_VERSION = 1; 
    public static final String KEY_ID = "_id"; 
    public static final String KEY_CONTENT = "Content"; 

    //create table MY_DATABASE (ID integer primary key, Content text not null); 
    private static final String SCRIPT_CREATE_DATABASE = 
    "create table " + MYDATABASE_TABLE + " (" 
    + KEY_ID + " integer primary key autoincrement, " 
    + KEY_CONTENT + " text not null);"; 

    private SQLiteHelper sqLiteHelper; 
    private SQLiteDatabase sqLiteDatabase; 

    private Context context; 

    public SQLiteAdapter(Context c){ 
     context = c; 
    } 

    public SQLiteAdapter openToRead() throws android.database.SQLException { 
     sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
     sqLiteDatabase = sqLiteHelper.getReadableDatabase(); 
     return this; 
    } 

    public SQLiteAdapter openToWrite() throws android.database.SQLException { 
     sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION); 
     sqLiteDatabase = sqLiteHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close(){ 
     sqLiteHelper.close(); 
    } 

    public long insert(String content){ 

     ContentValues contentValues = new ContentValues(); 
     contentValues.put(KEY_CONTENT, content); 
     return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues); 
    } 

    public int deleteAll(){ 
     return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null); 
    } 

    public Cursor queueAll(){ 
     String[] columns = new String[]{KEY_ID, KEY_CONTENT}; 
     Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, 
     null, null, null, null, null); 

     return cursor; 
    } 

    public List<Message> getAllMessages() { 
     String[] columns = new String[]{KEY_ID, KEY_CONTENT}; 
     List<Message> messages = new ArrayList<Message>(); 

     Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, 
       columns, null, null, null, null, null); 

     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      Message message = cursorToMessage(cursor); 
      messages.add(message); 
      cursor.moveToNext(); 
     } 
     // Make sure to close the cursor 
     cursor.close(); 
     return messages; 
     } 

    public class SQLiteHelper extends SQLiteOpenHelper { 

     public SQLiteHelper(Context context, String name, 
     CursorFactory factory, int version) { 
      super(context, name, factory, version); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      // TODO Auto-generated method stub 
      db.execSQL(SCRIPT_CREATE_DATABASE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 

     } 

    } 

     private Message cursorToMessage(Cursor cursor) { 
      Message message = new Message(); 
      message.setId(cursor.getLong(0)); 
      message.setMessage(cursor.getString(1)); 
      return message; 
     } 

} 

而這裏的消息。 java代碼:

package com.example.smsTest; 

public class Message { 
     private long id; 
     private String message; 

     public long getId() { 
     return id; 
     } 

     public void setId(long id) { 
     this.id = id; 
     } 

     public String getMessage() { 
     return message; 
     } 

     public void setMessage(String message) { 
     this.message = message; 
     } 

     // Will be used by the ArrayAdapter in the ListView 
     @Override 
     public String toString() { 
     return message; 
     } 
} 

的問題是String body = msg.substring(msg.lastIndexOf(":")+1, msg.length());值沒有更新到ListView時,該代碼火bodyarr.add(body); arrayAdpt.notifyDataSetChanged();。此代碼可以在SMSReceiverActivity類中找到。

這是工作之前,我添加了一個sqlite代碼。

回答

2

ListAdapter內擁有Message類型的對象,和現場的你Activity裏面的類型是List<String>。您將List<Message>傳遞到AdapteronCreate(),然後onReceive()您將String添加到List<String>。首先,這兩個列表具有不兼容的類型,因此您無法將List<String>傳遞給Adapter。第二,通過調用bodyarr.add(body),您正在向Activity's列表Strings添加項目,但不會以任何方式影響Adapter。所以你可能應該先解決類型不兼容問題,然後在你添加這個列表中的內容來更新Adapter中的數據之後,不要忘記調用adapter.setItems(bodyarr)。然後致電adapter.notifyDataSetChanged()將完成這項工作。希望這可以幫助。