How to get the primary key of an item in a sorted recycleview

0

I have a populated recycleview with a Realm bank, if I do not sort, my code works because it puts in the order of the ID (primary key). But I want to sort by alphabetical order, and when I do this my code is to open a dialog, it does not work because the click position is not the same as the id. How can I get the id when I click?

   realm = Realm.getDefaultInstance();
    adapter = new MeuAdpaterAlimentos(realm.where(Alimento.class).findAll(),true,true);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(),2);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    //recyclerView.setHasFixedSize(true);

    ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
        @Override
        public void onItemClicked(RecyclerView recyclerView, int position, View v) {
            Alimento alimento = realm.where(Alimento.class).equalTo("id",position).findFirst();
            //Log.d(TAG, "onItemClicked: "+ v.toString());
            criarnumberpickdialog(alimento.getMedida(),alimento.getNome(),alimento.getCarboidratos(),alimento.getCalorias(),alimento.getGordura(),alimento.getProteinas());
        }
    });

My Adpater:

package com.igoroliv.lifestyle.Telas.Alimentacao;


import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.igoroliv.lifestyle.Modelos.Alimento;
import com.igoroliv.lifestyle.R;

import io.realm.OrderedRealmCollection;
import io.realm.RealmRecyclerViewAdapter;

/**
 * Created by igord on 21/11/2017.
 */

class MeuAdpaterAlimentos extends RealmRecyclerViewAdapter<Alimento,MeuAdpaterAlimentos.VH> {

    public MeuAdpaterAlimentos(@Nullable OrderedRealmCollection<Alimento> data, boolean autoUpdate, boolean updateOnModification) {
        super(data, autoUpdate, updateOnModification);
    }


    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.linharecyclealimento, parent, false);
        return new VH(itemView);    }

    @Override
    public void onBindViewHolder(VH holder, int position) {
        final Alimento alimento = getItem(position);
        //holder.data = alimento;
        holder.titulo.setText(alimento.getNome());

        String str = alimento.getMedida();
        String[] strArray = str.split(" ");
        StringBuilder builder = new StringBuilder();
        for (String s : strArray) {
            String cap = s.substring(0, 1).toUpperCase() + s.substring(1);
            builder.append(cap + " ");
        }
        holder.medida.setText(builder.toString());
    }


    public class VH extends RecyclerView.ViewHolder {
        //public Alimento data;
        TextView titulo,medida;


        public VH(View itemView) {
            super(itemView);
            titulo =  itemView.findViewById(R.id.txv_alimentonome);
            medida =  itemView.findViewById(R.id.txv_alimentoMedida);

        }
    }
}
    
asked by anonymous 23.11.2017 / 19:54

1 answer

2

Implement the getItemId of the adapter, returning the id of its object. Example:

@Override
public long getItemId(int position) {
    return alimentos.get(position).getId();
}

However, this will only work if you sort the base list of the adapter as well.

    
23.11.2017 / 20:10