2012-08-06 115 views
0

以下代碼顯示一個錯誤味精,構造函數ArrayAdapter <String>(SmsActivity,int,SmsActivity.SmsReceived)未定義?

setListAdapter(new ArrayAdapter<String>(this, R.layout.main,mSmsReceiver)); 

錯誤味精:構造ArrayAdapter(SmsActivity,INT,SmsActivity.SmsReceived)是未定義

全碼在下文提供。

我的問題:

  1. 我怎麼能修復錯誤?
  2. 代替使用mSmsReceiver,如何/我應該怎麼修改顯示在STR(下面)消息的ListView

    str += "SMS from " + msgs[i].getOriginatingAddress(); 
    

完整代碼:

import... 
    .... 

    public class SmsActivity extends ListActivity { 
    private SmsReceived mSmsReceiver; 
    public SmsActivity(){ 
    mSmsReceiver = new SmsReceived(); 

    setListAdapter(new ArrayAdapter<String>(this, R.layout.main,mSmsReceiver)); 

    ListView listView = getListView(); 
    listView.setTextFilterEnabled(true); 

    //---method is call when listitem is clicked--- 
    listView.setOnItemClickListener(new OnItemClickListener() {edit later}); 
    } 


    private class SmsReceived extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     //---get the SMS message passed in--- 
     Bundle bundle = intent.getExtras();   
     SmsMessage[] msgs = null; 
     String str = "";  
     if (bundle != null) 
     { 
      //---retrieve the SMS message received--- 
      Object[] pdus = (Object[]) bundle.get("pdus"); 
      msgs = new SmsMessage[pdus.length];    
      for (int i=0; i<msgs.length; i++){ 
       msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
       str += "SMS from " + msgs[i].getOriginatingAddress();       

      } 
     } 
    } 
    } 
    } 

**對不起,因爲我這些東西真的太新了,所以希望你可以直接編輯我的代碼,以便我可以看到你想說的。我真的很抱歉,如果它確實給你帶來了很多負擔...

回答

1

要修復錯誤,需要正確的對象類型作爲適配器的參數。由於您告訴ArrayAdapter它將有String數據類型,因此您必須提供一個數組或列表集合對象,其類型爲String

更改此:

private SmsReceived mSmsReceiver; 

要這樣:

private String[] mSmsReceiver; 
//or 
private List<String> mSmsReceiver 

改變數據類型爲mSmsReceiver後,要確保你初始化並推到前填寫一些數據ArrayAdapter

0

@Waqas謝謝..但是,當我更改代碼(下面顯示)後,發生以下錯誤。

錯誤味精:類型不匹配:不能轉換從SmsActivity.SmsReceived爲String []

代碼BEFORE改性:

public class SmsActivity extends ListActivity { 
private SmsReceived mSmsReceiver; 
public SmsActivity(){ 
mSmsReceiver = new SmsReceived(); 

代碼AFTER改性:

public class SmsActivity extends ListActivity { 
private String[] mSmsReceiver; 
public SmsActivity(){ 
mSmsReceiver = new SmsReceived(); 
相關問題