gpt4 book ai didi

java - 通知数据集更改不起作用。当我收到短信时ListView没有更新

转载 作者:行者123 更新时间:2023-12-02 11:05:49 25 4
gpt4 key购买 nike

首先,我是一个初学者,我尝试对此进行研究,尝试实现一些解决方案,但无法使其发挥作用......

问题,

我希望在收到短信时立即更新 Activity ,我正在使用接收器并通知数据集更改。一切似乎都工作正常(当我收到短信时列表的大小会增加,它会通知适配器,但列表永远不会更新(如果我返回并再次打开 Activity ,它会更新))

这是我的接收器的代码...

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

这是我的 Activity 代码..

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

}

最佳答案

您将NotifyDataSetChanged放在onCreate中(这就是为什么它会在构建 Activity 时更新),您需要在refresh()函数中调用此函数。

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

关于java - 通知数据集更改不起作用。当我收到短信时ListView没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50967848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com