I have a two dimensional ArrayList
of type String
which is the input to the adapter class of the RecyclerView
. The data in the list is taken from an SQLite database everytime when onResume()
is called. I have implemented a drag and drop feature and an onMove()
function that swaps the list elements successfully. However, I need to store the modified list before onResume()
is called which will rewrite the list and fill it with old positions. My guess is to implement the rewriting functionality within the onStop()
event. This is my main activity:
RecyclerView recyclerView;
RecyclerViewAdapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<ArrayList<String>> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
assert fab != null;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, ViewDbActivity.class);
startActivity(i);
}
});
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
ItemTouchHelper item = new ItemTouchHelper(new ItemTouchHelper.Callback() {
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
int swipeFlags = ItemTouchHelper.RIGHT;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
Collections.swap(data, viewHolder.getAdapterPosition(), target.getAdapterPosition());
adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
return true;
}
@Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
Toast.makeText(MainActivity.this, "Moved", Toast.LENGTH_SHORT).show();
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
data.remove(viewHolder.getAdapterPosition());
adapter.notifyItemRemoved(viewHolder.getAdapterPosition());
}
});
item.attachToRecyclerView(recyclerView);
recyclerView.setItemAnimator(itemAnimator);
}
@Override
protected void onResume() {
super.onResume();
initialize();
if(adapter == null) {
Log.v(TAG, "Adapter = NULL. Initialized");
setAdapter();
}
else {
Log.v(TAG, "notify is called");
adapter.updateData(data);
}
}
public void initialize() {
Database ourDB = new Database(this);
ourDB.openDB();
data = ourDB.getData();
ourDB.closeDB();
}
public void setAdapter() {
adapter = new RecyclerViewAdapter(data, this);
recyclerView.setAdapter(adapter);
}
It is important to update the database as long as I know when to store the data. How can I solve this problem? Any suggestions?
!!!
ANSWER:
Final solution I have implemented:
@Override
public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
//preparing new position values
long movedItem = Long.parseLong(data.get(fromPos).get(0)); // .get(fromPos).get(0) returns PRIMARY KEY from the list
long draggedItem = Long.parseLong(data.get(toPos).get(0));
// updating affected rows with new orders
Database db = new Database(MainActivity.this);
db.open();
db.updateOrder(draggedItem, toPos);
db.updateOrder(movedItem, fromPos);
db.close();
}
And inside of DB class:
public void updateOrder(long rowID, int newPos) {
ContentValues cv = new ContentValues();
cv.put(KEY_ORDER, newPos);
ourDatabase.update(DATABASE_TABLE, cv, KEY_ROWID + "=" + rowID, null);
}