I have a fragment that contains a recyclerview where I add an onclick listener to each item inside the Adapter like below
public void onBindViewHolder(CardViewHolder holder, int position) {
// Get the current news item at the position
final News currentItem = mNewsList.get(position);
// Get the news title information from the current news item and
// set text on the news title {@link TextView}
holder.newsTitleTextView.setText(currentItem.getTitle());
// Get the news section information from the current news item
// and set text on the section {@link TextView}
holder.sectionNameTextView.setText(currentItem.getSection());
// Get the published date of the current news item information from the current news item
// and set the same as text on the date published {@link TextView}
holder.datePublishedTextView.setText(currentItem.getTestDate());
// Register and setup listener to open up news story in web browser
holder.storyCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Parse string as url object
//Uri webpage = Uri.parse(currentItem.getStoryUrl());
Intent i = new Intent(v.getContext().getApplicationContext(), NewsReader.class);
i.putExtra("image", currentItem.getThumbnail());
i.putExtra("title", currentItem.getTitle());
i.putExtra("body", currentItem.getSection());
i.putExtra("date", currentItem.getTestDate());
i.putExtra("url", currentItem.getStoryUrl());
v.getContext().getApplicationContext().startActivity(i);
}
});
// Check whether or not the current news item has a thumbnail or not
if (currentItem.getThumbnail() == null) {
// The current news item does not have thumbnail information
// Set scale type for the default image
holder.newsThumbnail.setScaleType(ImageView.ScaleType.CENTER);
// Set the default image on the {@link ImageView} for the thumbnail
holder.newsThumbnail.setImageResource(R.drawable.no_thumbnail);
} else {
// The current news item has thumbnail information
holder.newsThumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP);
// Get the bitmap thumbnail from the current news item and
// Set it as the image on the {@link ImageView} thumbnail
holder.newsThumbnail.setImageBitmap(currentItem.getThumbnail());
}
}
I'm getting the following runtime error everytime I click on one of the recyclerview items:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.thenews/com.example.android.thenews.NewsReader}: android.view.InflateException: Binary XML file line #0: ScrollView can host only one direct child