削除/追加などのデータベースで発生した変更を、recyclerViewにすぐに表示するにはどうすればよいですか?私はnotifyDataSetChanged()を試した。 arraylistからも直接削除され、再度データベースリストがリロードされました。しかし、これはすべて、面倒な回避策のように思えますし、作業もしません。ここで削除/追加時にRecyclerviewに表示されるデータベースの最新の変更を更新するにはどうすればいいですか?
は、それが今ある方法を示しています私のアプリで削除アクションのビデオです。
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();
}
}