如何将数据库中发生的更改(例如delete / add)立即显示在recyclerView中?我试过notifyDataSetChanged();并直接从arraylist中删除并重新加载数据库列表。但所有这些似乎都是一个混乱的解决方法,并且不起作用?
以下是我的应用中删除操作的视频,其中显示了它现在的状态。
https://www.youtube.com/watch?v=fCJnunOWBR4
这是用户点击来自recyclerview的活动时开始的活动:
public class EventActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_activity);
loadDatabaseData(this);
rvAdapter = new RvAdapter(databaseList, this);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.showOverflowMenu();
getSupportActionBar().setTitle(getIntent().getStringExtra("TITLE"));
pos = getIntent().getIntExtra("POS",0);
}
//Delete or remove happends here:---------------------
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_saveToDb:
DbHelper.getInstance(EventActivity.this).saveToDb(eventID, title, description, subtitle, start, end, 1, url, imgURL);
loadDatabaseData(getApplicationContext());
break;
case R.id.delete_from_Db:
DbHelper.getInstance(EventActivity.this).deleteEvent(eventID);
//these 3 lines dosn't help either
loadDatabaseData(this);
rvAdapter.notifyItemRemoved(pos);
rvAdapter.notifyDataSetChanged();
finish();
break;
default:
}
return super.onOptionsItemSelected(item);
}
public void loadDatabaseData(Context context) {
databaseList = new ArrayList<>();
dbHelper = new DbHelper(context);
sqLiteDatabase = dbHelper.getWritableDatabase();
Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM " + DbHelper.DATABASE_NAME, null);
EventCurserWrapper wrapper = new EventCurserWrapper(c);
if (c.moveToFirst()) {
while (!c.isAfterLast()) {
databaseList.add(new Event(wrapper.getEvent().getEventID(),wrapper.getEvent().getTitle(), wrapper.getEvent().subtitle, wrapper.getEvent().getDescrption(), wrapper.getEvent().startTime, wrapper.getEvent().endTime, wrapper.getEvent().url,wrapper.getEvent().getImgURL(), wrapper.getEvent().isDb));
c.moveToNext();
}
}
wrapper.close();
c.close();
}
}
这是具有recyclerView的MainActivity:
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
public RvAdapter rvAdapter;
private List<Event> databaseList, allEventsList, jsonList;
private DbHelper dbHelper;
private SQLiteDatabase sqLiteDatabase;
private Toolbar toolbar;
private SharedPreferences preferences;
private static final String URL = "http://events.makeable.dk/api/getEvents";
private static final String DESCRIPTION = "description_english";
private static final String TITLE = "title_english";
private static final String SUBTITLE = "subtitle_english";
private static final String PICTURE_URL = "picture_name";
private static final String EVENT_ID = "eventid";
private static final String DATES = "datelist";
private static final String EVENT_URL = "url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new DbHelper(this);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
int showListPref = preferences.getInt("SHOW_LIST", 3);
databaseList = new ArrayList<>();
jsonList = new ArrayList<>();
loadDatabaseData(this);
new JsonParser().execute();
toolbar = (Toolbar) findViewById(R.id.toolbar2);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.showOverflowMenu();
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Events in Arhus");
public void loadDatabaseData(Context context) {
dbHelper = new DbHelper(context);
sqLiteDatabase = dbHelper.getWritableDatabase();
Cursor c = sqLiteDatabase.rawQuery("SELECT * FROM " + DbHelper.DATABASE_NAME, null);
EventCurserWrapper wrapper = new EventCurserWrapper(c);
if (c.moveToFirst()) {
while (!c.isAfterLast()) {
databaseList.add(new Event(wrapper.getEvent().getEventID(),wrapper.getEvent().getTitle(), wrapper.getEvent().subtitle, wrapper.getEvent().getDescrption(), wrapper.getEvent().startTime, wrapper.getEvent().endTime, wrapper.getEvent().url,wrapper.getEvent().getImgURL(), wrapper.getEvent().isDb));
c.moveToNext();
}
}
wrapper.close();
c.close();
sqLiteDatabase.close();
}
public void initRecyclerView(List<Event> list) {
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
rvAdapter = new RvAdapter(list, this);
recyclerView.setAdapter(rvAdapter);
rvAdapter.notifyDataSetChanged();
}
}
答案 0 :(得分:0)
在我看来,这并不是解决问题的好办法。您可以拨打startActivityForResult()
而不是startActivity()
。
并将 EventActivity 的一些结果返回到 MainActivity ,如果需要,还可以在RecyclerView中重新加载数据。
您可以阅读有关Getting a Result from an Activity