-1

I have problem passing context into recyclerview adapter, please see my code below. How should I make Toast work in Onclick?

I also have problem starting new activity from this Onclick

    Intent intent = new Intent(context, Main2Activity.class);
    startActivity(intent);

as startActivity is not recognized as method. Any suggestions please?

My code:

public class MoviesAdapter extends RecyclerView.Adapter {

private List<Movie> moviesList;
private static final String TAG = "MyTestAppState";

Context context;

public MoviesAdapter(Context context, List<Movie> moviesList)  {
    this.context = context;
    this.moviesList = moviesList;
}


public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, year, genre;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.TXtitle);
        genre = (TextView) view.findViewById(R.id.TXgenre);
        year = (TextView) view.findViewById(R.id.TXyear);
    }
}


public MoviesAdapter(List<Movie> moviesList) {
    this.moviesList = moviesList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.movie_list_row, parent, false);

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(context,"Test", Toast.LENGTH_SHORT).show();

            Log.v(TAG, "TEST");

        }
    });

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    Movie movie = moviesList.get(position);
    holder.title.setText(movie.getTitle());
    holder.genre.setText(movie.getGenre());
    holder.year.setText(movie.getYear());
}

@Override
public int getItemCount() {return moviesList.size();
}

}

Michal
  • 11
  • 2
  • if the app is crashing, show your logcat and how you are creating the Recyclerview – OneCricketeer Apr 17 '17 at 06:31
  • First of all debug and check the context value you are passing. I suspect that it may be null. For toast messages you can use app context instead of activity context. – am110787 Apr 17 '17 at 06:40
  • I call Main2Activity from other activity and it works fine. Let`s concentrate on Toast only please. AFAIK problem is context. – Michal Apr 17 '17 at 06:42
  • @Michal please post your code from where you are calling your adapter... – Rahul Sharma Apr 17 '17 at 06:43
  • Toast.makeText(parent.getContext(), "Test" , Toast.LENGTH_SHORT).show(); – Satan Pandeya Apr 17 '17 at 06:45
  • @cricket_007 wow! many thanks for suggestion. I was looking a few hours looking into recyclerview context examples, but hadn`t found this one. – Michal Apr 17 '17 at 07:06
  • Also check in your Activity if you already initialized the Context before initializing the Adapter. – mariozawa Apr 17 '17 at 08:39
  • @mariozawa An Activity is a Context. What is there to initialize? – OneCricketeer Apr 17 '17 at 13:31
  • @cricket_007 When you initialize your Adapter make sure you have already a value for your context. Or you can do this. mMoviesAdapter = MoviesAdapter(getContext(), YOUR_LIST); or you can initialize the context before that line mContext = getContext(); – mariozawa Apr 17 '17 at 22:15
  • @mariozawa All I'm saying is that `mContext` not necessary. Fragments use `MoviesAdapter(getActivity()` and Activities use `MoviesAdapter(YourActivity.this` – OneCricketeer Apr 17 '17 at 22:26

2 Answers2

1

Use context.startActivity(intent); instead of startActivity(intent);

Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • my app crashes when I click Recycleview item. Without Toast and Intent, Log works fine so I suppose context is a problem. Any suggestions? – Michal Apr 17 '17 at 06:29
0

Try

context.startActivity(intent);

Make sure Main2Activity is added in AndroidManifest.xml

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53