I followed this tutorial ( http://www.startingandroid.com/how-to-use-sqlite-database-in-android/ ) as I am relatively new to Android development and wanted to get an idea of how SQL works. As I am looking to use the CardView and RecyclerView for an application that I am developing, that tutorial was very helpful in getting me started.
However, I have gone further and implemented the 'Contextual Action Bar' to be able to 'Favourite' or 'Delete' selected items (my code for this can be seen below). So, my question is, how can I retrieve the position of the selected item and extract the SQL ID from that and how would I go about incorporating multi-select with the CAB?
@Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_row, parent, false);
UserViewHolder userViewHolder = new UserViewHolder(v);
v.setClickable(true);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, "CLICK", Snackbar.LENGTH_SHORT).show();
}
});
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Snackbar.make(v, "LONG CLICK", Snackbar.LENGTH_SHORT).show();
if (mActionMode != null) {
return false;
}
v.startActionMode(new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.context, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//get number of selected items
switch (item.getItemId()) {
case R.id.context_favourite:
//SQL - Favourite Item
mode.finish();
case R.id.context_delete:
//SQL - Delete Item
mode.finish();
}
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
});
return true;
}
});
return userViewHolder;
}
I had the same problem, ended up saving the id from the cursor in the viewfinder. Do this in your createviewholder func
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.