【问题标题】:Updating a listview in Fragment更新片段中的列表视图
【发布时间】:2013-02-06 15:53:10
【问题描述】:

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


        }
    }
}

有人能解释一下我的问题吗? 非常感谢!

【问题讨论】:

  • 此外,您可能会看到现在发生的情况是 te Mainactivity.class 再次启动。在应用程序的此(第二次)生成中(收到短信时),列表将使用生成它的最后一条短信进行更新。但是,其他短信不会显示在列表中,只会出现在另一个新的生成中。

标签: android listview refresh fragment


【解决方案1】:

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

在你的片段声明中添加类似的东西:

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 类中包含这一切就是我所做的。

【讨论】:

  • 我无法添加适配器和list.setAdapter(适配器)这可能是因为接收器在另一个文件中吗?短信接收器.java?在不同的文件中还有另外两个类处理传入的短信并放入列表中: SmsInfo.java SmsInfoadapter 我从这个例子中得到了这些:xjaphx.wordpress.com/2011/07/14/… 也许这让我的问题更清楚一点?正如我所说,我对 android/java 还很陌生,所以一些基础知识对我来说可能(还)不太清楚......
  • 尽管我的 arduino 接收短信并在 25cm x 25cm 的 8x8 LED 屏幕上滚动它们,我认为这很棒:)
  • 就面向对象设计而言,最好为每个需要更新/修改 UI 元素的地方创建单独的 BroadcastReceiver 类。如果当前的已经为别的东西工作了,你可以把它移到那个使用它的地方,并为这个类创建一个单独的。这样,在 onReceive() 中,您只需调用一个方法/修改列表适配器并轻松列出。然后流程将是:发送广播 ---> 所有接收者都接收数据并修改他们需要的内容 ---> 快乐的程序员
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-20
  • 2021-09-27
  • 2013-04-15
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
相关资源
最近更新 更多