0

我有以下代码来显示列表视图中收到的消息:

package com.example.smsTest;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class SMSReceiverActivity extends ListActivity {
    private BroadcastReceiver mIntentReceiver;
    ListView listview;
    ArrayAdapter<String> arrayAdpt;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smsreceiver);

        listview=this.getListView();
    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN");
        mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String msg = intent.getStringExtra("get_msg");

            //Process the sms format and extract body &amp; phoneNumber
            msg = msg.replace("\n", "");
            String body = msg.substring(msg.lastIndexOf(":")+1, msg.length());
            String pNumber = msg.substring(0,msg.lastIndexOf(":"));

            //Add it to the list or do whatever you wish to
            ArrayList<String> bodyarr=new ArrayList<String>();
            bodyarr.add(body);
            arrayAdpt = new ArrayAdapter<String>(SMSReceiverActivity.this, android.R.layout.simple_list_item_1,
            bodyarr);           
            listview.setAdapter(arrayAdpt);
            arrayAdpt.notifyDataSetChanged();
        }
    };

    this.registerReceiver(mIntentReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.unregisterReceiver(this.mIntentReceiver);
    }

}

但是,问题是之前的消息被覆盖了。我尝试添加arrayAdpt.notifyDataSetChanged();代码无济于事。

我也在这里阅读了很多答案,但它不适用于我的代码。

请提供任何帮助。

4

1 回答 1

2

每次收到新消息时,您都在创建一个全新的列表,这就是为什么之前的消息总是被覆盖的原因。

相反,将 bodyarr 和 arrayAdpt 的声明移动到类字段,以便每次收到新消息时都可以共享和修改它们:

private ArrayList<String> bodyarr = new ArrayList<String>();
private ArrayAdapter<String> arrayAdpt;

在 onCreate 中,您应该为您的 ListView 设置列表适配器:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smsreceiver);

        listview = this.getListView();
        arrayAdpt = new ArrayAdapter<String>(SMSReceiverActivity.this, android.R.layout.simple_list_item_1,
        bodyarr);

        listview.setAdapter(arrayAdpt);
    }

然后,在您的广播接收器的 onReceive 方法中,您几乎可以执行您之前所做的操作,除了使用已经存在的列表和数组适配器,这样您已经添加的内容就不会被覆盖:

mIntentReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String msg = intent.getStringExtra("get_msg");

        //Process the sms format and extract body &amp; phoneNumber
        msg = msg.replace("\n", "");
        String body = msg.substring(msg.lastIndexOf(":")+1, msg.length());
        String pNumber = msg.substring(0,msg.lastIndexOf(":"));

        bodyarr.add(body);

        arrayAdpt.notifyDataSetChanged();
    }
于 2013-04-07T15:24:23.347 回答