0
I’m with a recycleview q takes the data from a database ( I’m using Sugar ORM ) and reversed the order because I wanted from the newest to the oldest, but to delete I need the position in the database, but I’m doing with Swipe, and it takes the position of adpater ( that is not the same of DB because it is inverted ), how to get the position so ?
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 the Consortium:
@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);
}
}
};
tried here and it didn’t work @Márcio, I added your code to the question, see if I applied it correctly
– Igor Oliveira
But does it give any error? What exactly did not work?
– Márcio Oliveira
Position of getadapterposition: 10 ( first item from top to bottom I did Swipe ) Position to delete : 0 Error: "Error deleting notification in DB: java.lang.Nullpointerexception
– Igor Oliveira
By error, your.findById() Notification method is returning null. You have to understand why. If you can, put it up there.
– Márcio Oliveira
It’s a standard ORM sugar method, but I’ll check it out, thinking about switching databases
– Igor Oliveira
I remade the bank but even so, giving the same error, I order him to delete the first from top to bottom that is notification 5, but the value passed by your code is the first from bottom to top.
– Igor Oliveira
LOG: 07-05 22:54:00.277 10904-10904/br.com.igoroliv.youtubecanal D/igr: posicao: 1
07-05 22:54:00.277 10904-10904/br.com.igoroliv.youtubecanal D/igr: positionToDelete: 0
07-05 22:54:00.277 10904-10904/br.com.igoroliv.youtubecanal D/SQL Log: Sqlitequery: SELECT * FROM NOTIFICATION WHERE id=? LIMIT 1 07-05 22:54:00.277 10904-10904/br.com.igoroliv.youtubecanal D/Igr: Error deleting notification in Dbjava.lang.Nullpointerexception
– Igor Oliveira
Debugging here I realized, that he’s passing null even, I don’t understand why
– Igor Oliveira
From what I saw in the documentation of this Sugar, the first BD index is 1, not 0, so you have to take the -1 of my reply. I will edit.
– Márcio Oliveira
Because I noticed also the -1, I had tested without it, and still the same thing
– Igor Oliveira
As I commented on your other question, try to implement the base using the native classes: Sqliteopenhelper and Sqlitedatabase
– Márcio Oliveira