How to get position of inverted RecycleView?

0

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

    }

}

};

    
asked by anonymous 05.07.2017 / 02:45

1 answer

1

You can set the position to delete in the database as follows:

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

where adapterSize is the total of items in the Adapter (RecyclerView) and adapterPosition is the position of the RecyclerView item you are swiping.

If you do not want to do any math, you can create a helper method in the Adapter that returns the id of the item in a certain position and call that method in onSwiped () to delete the item by id.

    
05.07.2017 / 03:48