Answer the question
In order to leave comments, you need to log in
Why isn't the startActivity method being called?
I make the following application logic: transition to another activity when the RecyclerView element is clicked. Listener added in onBindViewHolder, fires fine, test message appears after click. But when writing the code, the studio paints the startActivity method red, I can’t understand why. Please tell me the possible reasons, maybe someone has come across this / I quote the adapter code in full:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private List<Book> booksList;
public RecyclerAdapter(List<Book> booksList) {
this.booksList = booksList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.card, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
Book book = booksList.get(position);
/**
* Загрузка обложки из интернета. Сссылки в MainActivity
*/
Picasso
.with(holder.imgObl.getContext())
.load(book.getResId())
.into(holder.imgObl);
/**
* обработка нажатия на элемент списка RecyclerView
*/
holder.imgObl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(holder.imgObl.getContext(),
// "Пойдем читать " + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(holder.imgObl.getContext(), SliderActivity.class);
startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return booksList.size();
} //считает количество элементов в списке
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgObl;
CardView cv;
public ViewHolder(View itemView) {
super(itemView);
imgObl = (ImageView) itemView.findViewById(R.id.iv_recycler_item);
cv = (CardView) itemView.findViewById(R.id.card_view);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question