0

I am trying to build a news app where once the user clicks on a news item, it will open the news in another activity in details. But currently, I am having a problem trying to open an activity once the user clicks on any of the news items.

I am using a recycler view to parse the response I get from the JSON using volley for the internet connection, I have tried some of the examples already stated in StackOverflow but non seems to be working to my expectation.

@Override
    public void onBindViewHolder(@NonNull final newsAdapter.viewHolder viewHolder, int i) {

    dataModel dataModel = mDataModel.get(i);
    viewHolder.mTextView.setText(dataModel.getTitle());
    viewHolder.mTextDescrip.setText(dataModel.getDescrip());
    Glide.with(context).load(dataModel.getImage()).into(viewHolder.mImageView);

    viewHolder.mclickListener.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            context.startActivity(new Intent(context, newsActivity.class));

        }
    });

}


 public viewHolder(@NonNull View itemView) {

        super(itemView);
        mTextView = itemView.findViewById(R.id.layout_text);
        mImageView = itemView.findViewById(R.id.layout_image);
        mTextDescrip = itemView.findViewById(R.id.layout_descrip);
        mclickListener = (CardView) itemView.findViewById(R.id.recyclerviewlayout);
    }

I tried adding a setOnClickListener with a toast messages to display a message when the user clicks on an item; it shows the message but when I tried using intent to open another activity the app keeps crashing.

I'm not using an emulator to run this app so I can't get the exact error message that causes the app to crash.

yhyrcanus
  • 3,743
  • 2
  • 23
  • 28
Wami ikechukwu
  • 57
  • 2
  • 10
  • can you please the XML as well? – kunal manocha Sep 12 '19 at 11:59
  • If the answer below stopped your crash, then you're passing the wrong `Context` to your `Adapter`. You really probably do not want to start a new task for the next `Activity`. – Mike M. Sep 12 '19 at 12:26
  • @MikeM. i am really confuse here, the answer below solved the issue but seems i still have a much bigger problem because i want to display the news in full but since its in a different task, that cant happen. – Wami ikechukwu Sep 12 '19 at 12:40
  • @MikeM. when i tired using `this` or `getContext` it didnt work...do you have any idea on how to get the correct context? please i need this to work. – Wami ikechukwu Sep 12 '19 at 12:43
  • It really doesn't have anything to do with what you can or can't display in the next `Activity`. And it's not that you're using the wrong thing inside the `Adapter`. I mean that you're not using the correct `Context` when you do `new YourAdapter(...)`. You need to pass the `Activity` there, not `getApplication()`, or `getApplicationContext()`, or `getBaseContext()`, etc. – Mike M. Sep 12 '19 at 12:51
  • @MikeM. i really appreciate your help but i am a beginner and thus dont really understand what you said there. – Wami ikechukwu Sep 12 '19 at 13:02
  • Don't worry about it for now, then. Later, you might notice what effect starting a new task has, and if it's an issue, you can address it then. – Mike M. Sep 12 '19 at 13:05

1 Answers1

-1

you should add Intent.FLAG_ACTIVITY_NEW_TASK in case of opening activity from recycler view please check this code

@Override
    public void onBindViewHolder(@NonNull final newsAdapter.viewHolder viewHolder, int i) {

    dataModel dataModel = mDataModel.get(i);
    viewHolder.mTextView.setText(dataModel.getTitle());
    viewHolder.mTextDescrip.setText(dataModel.getDescrip());
    Glide.with(context).load(dataModel.getImage()).into(viewHolder.mImageView);

    viewHolder.mclickListener.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
        Intent newsIntent=new Intent(context, newsActivity.class);
        newsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(newsIntent);

        }
    });

}


 public viewHolder(@NonNull View itemView) {

        super(itemView);
        mTextView = itemView.findViewById(R.id.layout_text);
        mImageView = itemView.findViewById(R.id.layout_image);
        mTextDescrip = itemView.findViewById(R.id.layout_descrip);
        mclickListener = (CardView) itemView.findViewById(R.id.recyclerviewlayout);
    }
Emad Seliem
  • 608
  • 1
  • 4
  • 5
  • it worked, but i can see that what i was expecting maynot work, that is, to show the news details in full from each item clicked. – Wami ikechukwu Sep 12 '19 at 12:20