0

我对 android 编程和一般编程相当陌生。我在 web 和 stackoverflow 上搜索了解决方案,但似乎找不到。

我有一个在片段中处理不同选项卡的应用程序。我的片段之一包含一个列表视图。但是,列表视图不会更新或刷新。当我收到传入的短信时,它应该刷新。这是片段代码:

public class SmsSectionFragment extends Fragment {

    @SuppressWarnings("null")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View listView = inflater.inflate(R.layout.fragment_section_sms, container, false);

        ListView mListData = (ListView) listView.findViewById(R.id.lvData);
        TextView aantalSms = (TextView) listView.findViewById(R.id.aantalSms);

        ArrayList<SmsInfo> listSms = getIntent().getParcelableArrayListExtra("ListSMS");

        // check condition 
        if(listSms != null && listSms.size() > 0) {
                // set data to list
                SmsInfoAdapter adapter = new SmsInfoAdapter(getActivity(), listSms);
                mListData.setAdapter(adapter);
                adapter.setNotifyOnChange(true);
                int count = listSms.size();
                aantalSms.setText(String.valueOf(count));

        }

        return listView;
    }

短信的接收在其他三个类中处理,Receiver代码为:

package com.example.android.effectivenavigation;

进口...等

public class SmsReceiver extends BroadcastReceiver {

static ArrayList<SmsInfo> listSms = new ArrayList<SmsInfo>();
@Override
public void onReceive(Context context, Intent intent) {



    // get SMS map from intent
    Bundle extras = intent.getExtras();
    // a notification message
    String messages = "";
    if ( extras != null ) {
        // get array data from SMS
        Object[] smsExtra = (Object[]) extras.get( "pdus" ); // "pdus" is the key

        for ( int i = 0; i < smsExtra.length; ++i ) {
            // get sms message
            SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
            // get content and number
            String body = sms.getMessageBody();
            String adition = "  SMS::  ";
            String einde = " ::SMS";
            String sendme = adition + body + einde;
            String address = sms.getOriginatingAddress();
            // create display message
            messages += "SMS from " + address + " :\n";
            messages += body + "\n";


            //Send to Arduino
            Amarino.sendDataToArduino(context, DEVICE_ADDRESS, 'T', sendme);


            // store in the list
            listSms.add(new SmsInfo(address, body));
        }

        // better check size before continue
        if(listSms.size() > 0) {
            // notify new arriving message
            //Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
            // set data to send
            Intent data = new Intent(context, MainActivity.class);
            // new activity
            data.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            data.putParcelableArrayListExtra("ListSMS", listSms);
            // start
            context.startActivity(data);


        }
    }
}

有人可以对我的问题有所了解吗?提前谢谢了!

4

1 回答 1

0

在我看来,您实际上没有任何东西可以更新片段。您的广播接收器正在接收信息,但您从未将其添加到片段中。

在您的片段声明中添加类似的操作:

private SMSReceiver receiver;
private IntentFilter filter;

在你的 onCreateView 添加:

receiver = new SMSReceiver();
filter = new IntentFilter(SMSReceiver.YOUR_STRING_FILTER);
registerReceiver(receiver, filter);

然后在 SMSReceiver 类的 onReceive 中添加如下内容:

adapter = new SmsInfoAdapter (this, yourData);
list.setAdapter(adapter);

在 Fragment 类中拥有这一切就是我所做的。

于 2013-02-06T16:23:18.480 回答