0

以下代码显示错误消息,

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();                          

            }
        }
    }
    }
    }

**对不起,因为我对这些东西真的太陌生了,所以希望你可以直接在我的代码中编辑,这样我就可以看到你想说什么。如果给你带来了很多负担,我真的很抱歉...

4

1 回答 1

1

要修复错误,您需要正确的对象类型作为适配器的参数。由于您告诉ArrayAdapter它将具有String数据类型,因此您必须提供类型为的数组或列表集合对象String

改变这个:

private SmsReceived mSmsReceiver;

对此:

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

更改mSmsReceiver的数据类型后,请确保在推入ArrayAdapter之前对其进行初始化并填写一些数据。

于 2012-08-06T07:53:52.180 回答