0

Делаю такую логику приложения: переход в другую активность при нажатии элемента RecyclerView. Слушатель добавил в onBindViewHolder, срабатывает нормально, тестовое сообщение появляется после нажатия. Но при написании кода студия красит красным метод startActivity, не могу понять почему. Подскажите пожалуйста возможные причины, может кто сталкивался с таким/ Код адаптера привожу полностью:

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);
        }
    }

}
1
  • класс Adapter не имеет метода startActivity() поэтому студия и "красит красным", в программировании все строго - нельзя вызвать то, чего нет, магия тут бессильна. Возможные причины и способы их решения вам объявит сама студия, если кликнуть на "покрашенное", а затем нажать на появившуюся красную лампочку слева.
    – pavlofff
    9 сен 2017 в 0:51

1 ответ 1

1

context.startActivity(intent). По идее, context вы определяете.

1
  • помогло, спасибище! 7 сен 2017 в 14:43

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.