简体   繁体   English

notifyDataSetChanged不起作用。 我收到短信时的ListView未更新

[英]notifyDataSetChanged doesn't work. ListView when i receive SMS is not updated

First, I am a beginner, and I tried researching about this, tried to implement some solutions but couldn't make it work... 首先,我是一个初学者,我尝试对此进行研究,尝试实施一些解决方案,但无法使其正常工作...

The problem, 问题,

I want instant updating in the activity when i receive an SMS, I am using an Receiver and notifydatasetChanged. 我想在收到短信,正在使用Receiver和notifydatasetChanged时即时更新活动。 Everything seems to work fine(the size of the list increases when i receive an sms, it notifies the adapter, but the list is never updated(it updates if i go back and open the activity again)) 一切似乎都正常(当我收到短信时,列表的大小会增加,它会通知适配器,但列表永远不会更新(如果我返回并再次打开活动,它将更新))

Here is the code of my receiver... 这是我的接收器的代码...

public class SmsBroadcastReceiver extends BroadcastReceiver {

public SmsModel msg;
MessageListAdapter adapter;
ArraySms mSmsList;
public SmsListActivity inst;

public static final String SMS_BUNDLE = "pdus";


public void onReceive(Context context, Intent intent) {
    mSmsList = new ArraySms() {
    };
    mSmsList = PreferencesManager.getSmsList(context);
    Bundle intentExtras = intent.getExtras();
    if (intentExtras != null) {
        Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);

        for (int i = 0; i < sms.length; i++) {
            String format = intentExtras.getString("format");
            SmsMessage smsMessage = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], format);
            }

            String smsBody = smsMessage.getMessageBody().toString();
            String address = smsMessage.getOriginatingAddress();
            msg = new SmsModel();
            msg.setNoticeText(smsBody);
            msg.setPhoneNumber(address);
            mSmsList.getSmsList().add(msg);
            }
           PreferencesManager.addSmsList(mSmsList,context);
        inst = SmsListActivity.instance();
        inst.updateList();
    }
}}

And here is my activity code.. 这是我的活动代码。

public class SmsListActivity extends AppCompatActivity {

MessageListAdapter mSmsAdapter;
@BindView(R.id.recyclerview_sms_list)
RecyclerView mRecyclerView;
private static SmsListActivity inst;
ArraySms arraySms;
SmsModel smsModel;

public static SmsListActivity instance() {
    return inst;
}

@Override
public void onStart() {
    super.onStart();
    inst = this;
    mSmsAdapter.notifyDataSetChanged();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sms_list);
    ButterKnife.bind(this);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    smsModel = new SmsModel();


    refreshSmsInbox();

    mSmsAdapter = new MessageListAdapter(this, arraySms);
    mRecyclerView.setAdapter(mSmsAdapter);
}

public ArraySms refreshSmsInbox() {
    arraySms = new ArraySms();
    arraySms.setSmsList(new ArrayList<>());
    ContentResolver contentResolver = getContentResolver();
    int permissionGranted = ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS);
    if (permissionGranted == PackageManager.PERMISSION_GRANTED) {
        Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);
        int indexBody = smsInboxCursor.getColumnIndex("body");
        int indexAddress = smsInboxCursor.getColumnIndex("address");
        while (smsInboxCursor != null && smsInboxCursor.moveToNext()) {
            smsModel = new SmsModel();
            smsModel.setPhoneNumber(smsInboxCursor.getString(indexAddress));
            smsModel.setNoticeText(smsInboxCursor.getString(indexBody));
            arraySms.getSmsList().add(smsModel);
        }
        if (smsInboxCursor != null) {
            smsInboxCursor.close();
        }

    }
    PreferencesManager.addSmsList(arraySms, SmsListActivity.this);
    return arraySms;


}
public void updateList() {
    arraySms = PreferencesManager.getSmsList(SmsListActivity.this);
    mSmsAdapter = new MessageListAdapter(SmsListActivity.this, arraySms);
    mRecyclerView.setAdapter(mSmsAdapter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
}

} }

You put the NotifyDataSetChanged in the onCreate (This is why it updates when the activity is built), you need to call this function in your refresh() function. 您将NotifyDataSetChanged放在onCreate中(这就是为什么在创建活动时更新它的原因),您需要在refresh()函数中调用此函数。

public void updateList() {
arraySms = PreferencesManager.getSmsList(SmsListActivity.this);
mSmsAdapter = new MessageListAdapter(SmsListActivity.this, arraySms);
mRecyclerView.setAdapter(mSmsAdapter);
mSmsAdapter.notifyDataSetChanged();
}

You should not recreate your Adapter whenever you want to update the item. 每当您要更新项目时,都不应重新创建适配器。 Instead, you need to swap the item to your Adapter. 相反,您需要将项目交换到适配器。 You can achieve it by modifying your Adapter to become something like this: 您可以通过将适配器修改为如下形式来实现:

public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.ViewHolder> {

  public void swap(ArraySms arraySms) {
    // clearing the list
    ArrayList<> list = this.arraySms.getSmsList();
    list.clear()
    // then readd from arraySms
    list.addAll(arraySms.getSmsList());

    notifyDataSetChanged();
  }
}

then you can use it inside updateList method: 然后可以在updateList方法中使用它:

public void updateList() {
    arraySms = PreferencesManager.getSmsList(SmsListActivity.this);
    mSmsAdapter.swap(arraySms);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM