简体   繁体   中英

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. 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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