C
C
Comatu2017-09-07 17:07:44
Android
Comatu, 2017-09-07 17:07:44

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

2 answer(s)
C
Comatu, 2017-09-07
@Comatu

holder.imgObl.getContext().startActivity(intent);

M
mitaichik, 2017-09-07
@mitaichik

Hover your mouse over the redness and the study itself will explain everything to you. And so - the adapter does not have a startActivity method. And since this is not some kind of internal activity class, there is nowhere to call it from.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question