A
A
apr1cot2019-11-25 14:47:00
Java
apr1cot, 2019-11-25 14:47:00

RecyclerAdapter - How to get and pass the entry ID to a new activity correctly?

Good afternoon. Tell me please. Now, when I click on an item, I get its position, but I would also like to get the post ID, and then pass it to the new activity. How can this be implemented?
5ddbbe23247ff005075977.png
I have the following code:
RecyclerActivity

public class RecyclerActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager layoutManager;
    public ArrayList<Brand> brands;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);

        ApiService api = RetroClient.getApiService();

        Call<Brands> call = api.getBrands();

        call.enqueue(new Callback<Brands>() {
            @Override
            public void onResponse(Call<Brands> call, Response<Brands> response) {
                brands = response.body().getBrands();
                // Log.d("BRANDS", new Gson().toJson(brands));

                RecyclerView recyclerBrands = (RecyclerView) findViewById(R.id.recyclerView);
                RecyclerAdapter adapter = new RecyclerAdapter(brands, RecyclerActivity.this);
                recyclerBrands.setAdapter(adapter);
                recyclerBrands.setLayoutManager(new LinearLayoutManager(RecyclerActivity.this));

                /**
                 * Add listener to every recycler view items
                 */
                recyclerBrands.addOnItemTouchListener(new CustomRVItemTouchListener(RecyclerActivity.this, recyclerBrands, new RecyclerViewItemClickListener() {
                    @Override
                    public void onClick(View view, int position) {
                        Snackbar.make(findViewById(R.id.layoutMain), "onClick at position : " + position, Snackbar.LENGTH_LONG).show();
                    }

                    @Override
                    public void onLongClick(View view, int position) {
                        Snackbar.make(findViewById(R.id.layoutMain), "onLongClick at position : " + position, Snackbar.LENGTH_LONG).show();
                    }
                }));
            }

            @Override
            public void onFailure(Call<Brands> call, Throwable t) {
                Log.d("onFailure", "onFailure: ");
            }
        });
    }
}

and RecyclerAdapter
public class RecyclerAdapter extends RecyclerView.Adapter<BrandHolder> {

    private List<Brand> list = Collections.emptyList();
    private Context context;

    public RecyclerAdapter(List<Brand> list, Context context){
        this.list = list;
        this.context = context;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public BrandHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View view = LayoutInflater.from(
                parent.getContext()).inflate(R.layout.recycler_row_layout, parent, false);

        return new BrandHolder(view);
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(BrandHolder holder, int position) {
        holder.txtTitle.setText(list.get(position).getTitle());
        holder.txtPrice.setText(list.get(position).getPrice());
        holder.txtPercent.setText(list.get(position).getPercent());
    }

    // Return the size of your
    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
apr1cot, 2019-11-25
@apr1cot

Solution to my problem, for those who face the same problem.

public class BrandHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    public String    id;
    public CardView  cardBrand;
    public TextView  txtTitle;

    public BrandHolder(View itemView) {
        super(itemView);

        itemView.setOnClickListener(this);

        //String itView = String.valueOf(itemView);

        cardBrand   = (CardView)  itemView.findViewById(R.id.cardBrand);
        txtTitle    = (TextView)  itemView.findViewById(R.id.txtTitle);
    }

    @Override
    public void onClick(View v) {
        Log.d("ITEM", String.valueOf(this.id));
        Snackbar.make(cardBrand, "onClick at position : " + getLayoutPosition(), Snackbar.LENGTH_LONG).show();
    }
}

D
Denis Zagaevsky, 2019-11-25
@zagayevskiy

I do not see the installation of a click listener in the adapter. When it is there, you can grab a specific Brand into it and then throw it further (I think there should be an id inside it).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question