I'm using a recycleview that takes data from a database (I'm using Sugar ORM) and I've reversed the order because I wanted the older ones for the older ones, but to delete I need the position in the database, I'm doing with swipe, and it picks up the adpater's position (which is not the same as the DB because it's inverted), how do I get the position like this?
List<Notificacao> listnotificacoes = Notificacao.listAll(Notificacao.class);
Log.d("igr","Lista de Notificaoes: " + listnotificacoes);
LinearLayoutManager llm = new LinearLayoutManager(view.getContext());
llm.setReverseLayout(true);
llm.setStackFromEnd(true);
mrecyclerView.setLayoutManager(llm);
mAdapterNoti = new AdapterNotificacao(listnotificacoes);
mrecyclerView.setAdapter(mAdapterNoti);
ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
final int position = viewHolder.getAdapterPosition();
Log.d("igr","posicao"+position);
int posicaodb = position+1;
Log.d("igr","posicaodb"+posicaodb);
try{
try{
Notificacao notificacao = Notificacao.findById(Notificacao.class,posicaodb);
Log.d("igr",""+notificacao.getMensagem());
}catch (Exception e){
Log.d("igr","erro ao obter mensagem da notificao" + e);
}
//notificacao.delete();
//Log.d("igr","deletou a notificao no BD");
//mAdapterNoti.notifyItemRemoved(position);
}catch (Exception e){
Log.d("igr","Erro ao deletar a notificacao no DB" + e);
}
}
};
Code proposed by Marcio:
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
final int position = viewHolder.getAdapterPosition();
Log.d("igr","posicao"+position);
int positionToDelete = mAdapterNoti.getItemCount() - position - 1;
//int posicaodb = position+1;
Log.d("igr","positionToDelete"+positionToDelete);
try{
try{
Notificacao notificacao = Notificacao.findById(Notificacao.class,positionToDelete);
Log.d("igr",""+notificacao.getMensagem());
}catch (Exception e){
Log.d("igr","erro ao obter mensagem da notificao" + e);
}
//notificacao.delete();
//Log.d("igr","deletou a notificao no BD");
//mAdapterNoti.notifyItemRemoved(position);
}catch (Exception e){
Log.d("igr","Erro ao deletar a notificacao no DB" + e);
}
}
};