How to get the Primary key of an item in an orderly recycleview

Asked

Viewed 43 times

0

I have a recycleview populated with a Realm bank, if I don’t sort, my code works because it puts in the ID order ( Primary key ). However I want to sort alphabetically, and when I do that my code is to open a dialog, does not work because the click position is not the same as the id. How can I get the id when 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);

        }
    }
}
  • 1

    Like the MeuAdpaterAlimentos was implemented?

  • @Leonardolima I edited and added

1 answer

2


Implement the Adapter getItemId, returning the id of your object. Example:

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

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

  • It worked, but I had to do it like this: food = getItem(position); Return food.getId();

  • It’s because the list I always work with her overall, but if it worked ta blz.

Browser other questions tagged

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