How to get inverted Recycleview position?

Asked

Viewed 223 times

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);

    }

}

};

1 answer

1


You can set the position to delete in the database this way:

int positionToDelete = adapterSize - adapterPosition;
// supondo que o índice do primeiro registro é 1, conforme documentação do Sugar.

where adapterSize is the total of Adapter (Recyclerview) and adapterPosition items is the position of the Recyclerview item that you are doing the Swipe.

If you don’t want to do any math, you can create an auxiliary method in the Adapter that returns the item id at a certain position and call this method in onSwiped() to delete the item by id.

  • tried here and it didn’t work @Márcio, I added your code to the question, see if I applied it correctly

  • But does it give any error? What exactly did not work?

  • 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

  • By error, your.findById() Notification method is returning null. You have to understand why. If you can, put it up there.

  • It’s a standard ORM sugar method, but I’ll check it out, thinking about switching databases

  • 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.

  • LOG: 07-05 22:54:00.277 10904-10904/br.com.igoroliv.youtubecanal D/igr: posicao: 1&#xA;07-05 22:54:00.277 10904-10904/br.com.igoroliv.youtubecanal D/igr: positionToDelete: 0&#xA;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

  • Debugging here I realized, that he’s passing null even, I don’t understand why

  • 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.

  • Because I noticed also the -1, I had tested without it, and still the same thing

  • As I commented on your other question, try to implement the base using the native classes: Sqliteopenhelper and Sqlitedatabase

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.