Добрый день. Подскажите, пожалуйста. Сейчас при клике на item я получаю его position, а мне бы хотелось еще получить ID записи, и потом передать его в новое активити. Как такое можно реализовать?
Имеется следующий код:
RecyclerActivitypublic 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: ");
}
});
}
}
и
RecyclerAdapterpublic 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);
}
}