我的应用在活动中包含ListView
和BroadcastReciever
。我希望Broadcastreciever
在触发时ListView
刷新listview
。我的接收器将内容放入sqlite数据库,我的lv=(ListView) findViewById(R.id.conversation);
lv.setSelection(lv.getCount()-1);
lv.setAdapter(new mAdapter(this, srsList, messagesList, timesList, numbersList, idList));
registerForContextMenu(lv);
显示该数据库的内容。
使用ListView的活动:
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(Messages.KEY_ID, i1);
values.put(Messages.KEY_sr, "r");
values.put(Messages.KEY_message, message);
values.put(Messages.KEY_time, datetime);
values.put(Messages.KEY_number, sen);
// Inserting Row
long student_Id = db.insert(Messages.TABLE, null, values);
db.close(); // Closing database connection
BroadcastReciever:
for bookerName,bookingId in zip(bookerNames,bookingIds):
Person.update_one({'Name': bookerName}, {'$push': {'Bookings': bookingId}})
答案 0 :(得分:0)
您可以在活动中创建一个BroadcastReciever,它包含适配器并通知应该刷新适配器:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
// String message = intent.getStringExtra("message");
if (mAdapter!= null) {
mAdapter.notifyDataSetChanged();
}
}
};
在onResume中重新启动此BroadcastReciever:
this.registerReceiver(mMessageReceiver, new IntentFilter("some_unique_name"));
和unregiester onPause:
this.unregisterReceiver(mMessageReceiver);
在您的BroadcastReciever中使用:
private void updateYourActivity(Context context) {
Intent intent = new Intent("some_unique_name");
// put whatever data you want to send, if any
// intent.putExtra("message", message);
// send broadcast
context.sendBroadcast(intent);
}