在Fragment中更新列表视图

时间:2013-02-06 15:53:11

标签: android listview refresh fragment

我对Android和编程的编程很新。我搜索了网络和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;
    }

接收短信的方法是在其他三个类中处理,接收方代码是:

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


        }
    }
}

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

1 个答案:

答案 0 :(得分: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类中就是我所做的。