I'm using RecyclerView with a custom Adapter but it's not showing data.
Adapter downloads some thumbnail images from database and get titles and infos from Api key.
Code of the Adapter Class:
public class mostViewedAdapter extends RecyclerView.Adapter<mostViewedAdapter.ViewHolder> {
private List<String> fTitle;
private List<String> fInfos;
private List<String> thumbs;
public mostViewedAdapter(List<String> titoli, List<String> infos, List<String>thumbsUrl){
fTitle = titoli;
fInfos = infos;
thumbs = thumbsUrl;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView infoTextView;
public ImageView thumbImageView;
public ViewHolder(View itemView) {
super(itemView);
titleTextView = (TextView) itemView.findViewById(R.id.title);
infoTextView = (TextView) itemView.findViewById(R.id.infos);
thumbImageView = (ImageView) itemView.findViewById(R.id.thumb);
}
}
#Override
public mostViewedAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View mvView = inflater.inflate(R.layout.most_viewed, parent, false);
ViewHolder viewHolder = new ViewHolder(mvView);
return viewHolder;
}
#Override
public void onBindViewHolder(mostViewedAdapter.ViewHolder holder, int position) {
String titolo = fTitle.get(position);
String info = fInfos.get(position);
String thumb = thumbs.get(position);
UrlGenerator url = new UrlGenerator(thumb);
Picasso.with(holder.thumbImageView.getContext()).load(url.generateImgUrl())
.resize(50,70).centerCrop().into(holder.thumbImageView);
holder.infoTextView.setText(info);
holder.titleTextView.setText(titolo);
TextView tvTitolo = holder.titleTextView;
TextView tvInfo = holder.infoTextView;
ImageView ivImage = holder.thumbImageView;
}
#Override
public int getItemCount() {
return 0;
}
}
Recycler view:
public void initUI() {
RecyclerView recList = (RecyclerView) findViewById(R.id.rvCinema);
recList.hasFixedSize();
mostViewedAdapter adapter = new mostViewedAdapter(titoli, info, thumbsUrl);
recList.setAdapter(adapter);
RecyclerView.LayoutManager lm = new LinearLayoutManager(this);
recList.setLayoutManager(lm);
}
try like this:
MainActivity
public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
mostViewedAdapter mViewAdapter;
//.... other elements
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_ID);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mViewAdapter= new mostViewedAdapter (listName, listInfo, listThumbs);
mRecyclerView.setAdapter(mViewAdapter);
}
}
Adapter Class
Instead of returning 0 as a count return the size of array of of any one of the three lists.
#Override
public int getItemCount() {
return fTitle.size();
//return fInfos.size();
//return thumbs.size();
}
Hope i helped, if still facing any issue, kindly post the logcat.
Please return fTitle.size();
#Override
public int getItemCount() {
return fTitle.size();
}
Related
I made a simple RecyclerView, but it do not load images!
Here are my codes. RecyclerView has 11 raws and each raw has 2 textviews and 1 imageview,
This is the important part of main activity,
MainActivity.java:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[] F_Name, D_Name;
int[] Img_res = {R.drawable.a_beautiful_mind,
R.drawable.a_seperation,
R.drawable.casablanca,
R.drawable.daei_jan_napelon,
R.drawable.game_of_thrones,
R.drawable.life_of_pie,
R.drawable.the_dictator,
R.drawable.the_godfather,
R.drawable.the_martian,
R.drawable.the_revenant,
R.drawable.v_for_vendetta};
ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.myRecyclerView);
F_Name = getResources().getStringArray(R.array.Film_Names);
D_Name = getResources().getStringArray(R.array.Director_Names);
int i = 0;
for (String name : F_Name){
DataProvider dataProvider = new DataProvider(Img_res[i], name, D_Name[i]);
arrayList.add(dataProvider);
i++;
}
adapter = new RecyclerAdapter(arrayList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
Here is my Adapter, I added 11 raws, and each raw contains 2 textviews and 1 ImageView,
Adapter:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private ArrayList<DataProvider> arrayList = new ArrayList<DataProvider>();
public RecyclerAdapter(ArrayList<DataProvider> arrayList){
this.arrayList = arrayList;
}
#Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recyclerview_raws, parent, false);
RecyclerViewHolder recyclerViewHolder = new RecyclerViewHolder(view);
return recyclerViewHolder;
}
#Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
DataProvider dataProvider = arrayList.get(position);
holder.imageView.setImageResource(dataProvider.getImg_res());
holder.f_name.setText(dataProvider.getF_name());
holder.d_name.setText(dataProvider.getD_name());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
TextView f_name, d_name;
public RecyclerViewHolder(View view){
super(view);
imageView = (ImageView)view.findViewById(R.id.myRecyclerViewImage);
f_name = (TextView)view.findViewById(R.id.myTextVFilmName);
d_name = (TextView)view.findViewById(R.id.myTextVDirectorName);
}
}
}
Can anyone help me please?
in onLoadFinsih i get back from the server List of movies i want to
add this list to my RecyclerView adapter
i tried to create List of movies and pass it to my RecyclerView adapter
then when i get the list of movies from the server in onLoadFinish method i add
this list to the list that i passed in the adapter then notify the adapter
but not working
public class MainActivity extends AppCompatActivity
implements LoaderManager.LoaderCallbacks<List<Movie>>{
private RecyclerView.Adapter movieRecyclerViewAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<Movie> mListOfMovies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mListOfMovies = new ArrayList<>();
movieRecyclerViewAdapter = new MovieItemAdapter(this, mListOfMovies);
mLayoutManager = new LinearLayoutManager(this);
recyclerView.setAdapter(movieRecyclerViewAdapter);
}
#Override
public void onLoadFinished(Loader<List<Movie>> loader, List<Movie> movies) {
//hide progress bar after fetch data from server
progressBar.setVisibility(View.GONE);
mListOfMovies.clear();
mListOfMovies.addAll(movies);
movieRecyclerViewAdapter.notifyItemRangeChanged();
}
public class MovieItemAdapter extends RecyclerView.Adapter<MovieItemAdapter.ViewHolder> {
private List<Movie> mListOfMovies;
private Context mContext;
public MovieItemAdapter(Context context, List<Movie> movies) {
this.mListOfMovies = movies;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext)
.inflate(R.layout.movie_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Movie movieItem = mListOfMovies.get(position);
holder.moviePoster.setImageBitmap(movieItem.getBitmap());
holder.movieName.setText(movieItem.getTitle());
DecimalFormat format = new DecimalFormat("0.0");
holder.voteAverage.setText(format.format(movieItem.getVoteAverage()));
holder.releaseDate.setText(movieItem.getReleaseDate());
}
#Override
public int getItemCount() {
return mListOfMovies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView moviePoster;
public TextView movieName;
public TextView voteAverage;
public TextView releaseDate;
public ViewHolder(View itemView) {
super(itemView);
moviePoster = (ImageView) itemView.findViewById(R.id.movie_poster);
movieName = (TextView) itemView.findViewById(R.id.movie_name);
voteAverage = (TextView) itemView.findViewById(R.id.vote_average);
releaseDate = (TextView) itemView.findViewById(R.id.release_date);
}
}
}
In onLoadFinish() you should update the list in your adapter with the received data.
Then you should call the adapter's adapter.notifyDataSetChanged().
EDIT:
Create a method in adapter like
setMovieList(List<Movie> movies){
mListOfMovies = movies;
this.notifyDataSetChanged();
}
and call it from onLoadFinished():
movieRecyclerViewAdapter.setMovieList(mListOfMovies)
Make your ViewHolder class non static in adapter class. Then check again.
So I have a CardView in a RecyclerView which has some data inside it. I would like to click on a specific card and launch a new fragment that displays specific data from the data in the CardView (more details about the data in the CardView). Here is the relevant code.
I don't have the most experience in programming and Android in general, any help would be appreciated!
DetailsFragment: `
public class DetailsFragment extends Fragment {
private CardView cardView;
private RecyclerView rv;
private List<Detail> detailList = new ArrayList<>();
public DetailsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_details, container, false);
Context context = getActivity();
cardView = (CardView) rootView.findViewById(R.id.cv);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
final LinearLayoutManager llm = new LinearLayoutManager(context);
rv.setLayoutManager(llm);
RVDetailAdapter adapter = new RVDetailAdapter(detailList);
rv.setAdapter(adapter);
initializeData();
initializeAdapter();
return rootView;
}
private void initializeData() {
detailList = new ArrayList<>();
// not sure here
detailList.add(new Detail(" "));
}
private void initializeAdapter(){
RVDetailAdapter adapter = new RVDetailAdapter(detailList);
rv.setAdapter(adapter);
}
}
RVNewsAdapater:
public class RVNewsAdapter extends RecyclerView.Adapter<RVNewsAdapter.NewsViewHolder> {
public static final String KEY_LINK ="link";
public static class NewsViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener {
private static final String TAG = "Hello";
CardView cv;
TextView date;
TextView link;
TextView title;
TextView today;
public NewsViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
date = (TextView) itemView.findViewById(R.id.date);
link = (TextView) itemView.findViewById(R.id.link);
title = (TextView) itemView.findViewById(R.id.heading);
today = (TextView) itemView.findViewById(R.id.today);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Context context = view.getContext();
Intent intent = null;
for(int postion = 0; postion<42; postion++) {
intent = new Intent(context, Details.class);
}
context.startActivity(intent);
}
}
List<News> news;
RVNewsAdapter(List<News> news) {
this.news = news;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public RVNewsAdapter.NewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
NewsViewHolder nvh = new NewsViewHolder(v);
return nvh;
}
#Override
public void onBindViewHolder(NewsViewHolder newsViewHolder, int i) {
newsViewHolder.date.setText(news.get(i).date);
newsViewHolder.link.setText(news.get(i).link);
newsViewHolder.title.setText(news.get(i).title);
newsViewHolder.today.setText(news.get(i).today);
}
#Override
public int getItemCount() {
if (news != null) {
return news.size();
}
return 0;
}
}
RVDetailAdapter:
public class RVDetailAdapter extends RecyclerView.Adapter<RVDetailAdapter.DetailViewHolder> {
public static class DetailViewHolder extends RecyclerView.ViewHolder {
TextView link;
public DetailViewHolder(View itemView) {
super(itemView);
link = (TextView) itemView.findViewById(R.id.link);
}
}
List<Detail> detail;
RVDetailAdapter(List<Detail> detail) {
this.detail = detail;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public RVDetailAdapter.DetailViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemnewsfeed, viewGroup, false);
DetailViewHolder dvh = new DetailViewHolder(v);
return dvh;
}
#Override
public void onBindViewHolder(DetailViewHolder detailViewHolder, int i) {
detailViewHolder.link.setText(detail.get(i).link);
}
#Override
public int getItemCount() {
if (detail != null) {
return detail.size();
}
return 0;
}
}
Not sure if you're still looking for an answer, but I just saw the post and thought I'd give you one option.
When I'm using a RecyclerView (doesn't matter if it's using the CardView or not, it's the same for any list item), I create a a click listener for the view item in the Adapter. When the user clicks the item/cardview, I dispatch an item click event. It's your choice on how you respond - you can use a standard callback interface (and pass in your listener when you create the adapter) or use something like EventBus.
Here's an example of the code you would add to your adapter class:
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemClick(view);
}
});
return new ViewHolder(view);
}
private void itemClick(View view) {
//Handle item event here
//EventBus.getDefault().post(new ItemSelectedEvent(/*send apprpopriate data*/));
}
I am fetching data from TMDB API.
I am getting
error:RecyclerView: No adapter attached; skipping layout
Right now, I am getting data via Retrofit.
Can anybody help me?
Adapter code is bellow:
public class ReviewAdapter extends RecyclerView.Adapter<MovieDetailActivity.MovieViewHolder> {
private List<Reviews> rReviewList;
private LayoutInflater rInflater;
private Context rContext;
public ReviewAdapter(Context context) {
this.rContext = context;
this.rInflater = LayoutInflater.from(context);
this.rReviewList = new ArrayList<>();
}
public MovieDetailActivity.MovieViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
// create a new view
View view = rInflater.inflate(R.layout.row_review, parent, false);
return new MovieDetailActivity.MovieViewHolder(view);
}
#Override
public void onBindViewHolder(MovieDetailActivity.MovieViewHolder holder, int position) {
Reviews reviews = rReviewList.get(position);
Picasso.with(rContext)
.load(reviews.getUrl())
.into(holder.imageView);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Perform click
}
});
}
#Override
public int getItemCount() {
return rReviewList.size();
}
//To update data
public void setReviewList(List<Reviews> reviewsList) {
this.rReviewList = new ArrayList<>();
this.rReviewList.addAll(reviewsList);
notifyDataSetChanged();
}
}
MovieDetailsActivity is where I want to fetch review data
Its code is below:
public class MovieDetailActivity extends AppCompatActivity {
public static final String EXTRA_MOVIE = "EXTRA_MOVIE";
private Movie mMovie;
private Reviews mReviews;
private Genres mGenres;
public ReviewAdapter rAdapter;
//private MoviesAdapter.ReviewAdapter rAdapter;
ImageView backdrop;
ImageView poster;
TextView title;
TextView description;
TextView releaseDate;
TextView voteAverage;
RecyclerView reviews;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movie_detail);
if (getIntent().hasExtra(EXTRA_MOVIE)) {
mMovie = getIntent().getParcelableExtra(EXTRA_MOVIE);
} else {
throw new IllegalArgumentException("Detail activity must receive a movie parcelable");
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CollapsingToolbarLayout toolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
toolbarLayout.setTitle(mMovie.getTitle());
//ucitavanje adaptera
reviews = (RecyclerView) findViewById(R.id.reviewList);
reviews.setAdapter(rAdapter);
rAdapter = new ReviewAdapter(MovieDetailActivity.this);
getReviews(mMovie.getId());
backdrop = (ImageView) findViewById(R.id.backdrop);
title = (TextView) findViewById(R.id.movie_title);
description = (TextView) findViewById(R.id.movie_description);
releaseDate = (TextView) findViewById(R.id.movie_date);
voteAverage = (TextView) findViewById(R.id.movie_rating);
//genres = (ListView) findViewById(R.id.genresArray);
title.setText(mMovie.getTitle());
description.setText(mMovie.getDescription());
voteAverage.setText(mMovie.getVoteAverage());
releaseDate.setText(mMovie.getReleaseDate());
poster = (ImageView) findViewById(R.id.movie_poster);
String internetUrl = "http://image.tmdb.org/t/p/w500";
Glide.with(this)
.load(mMovie.getPoster())
.override(800, 890)
.into(poster);
Glide.with(this)
.load(mMovie.getBackdrop())
.into(backdrop);
}
private void getReviews(String id) {
RestAdapter.getMovieService().getReviews(id, new Callback<ReviewWraper>() {
#Override
public void success(ReviewWraper reviewWraper, Response response) {
rAdapter.setReviewList(reviewWraper.getResults());
}
#Override
public void failure(RetrofitError error) {
error.printStackTrace();
}
});
}
public static class MovieViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public MovieViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.picture_review);
}
}
}
Add/edit this lines
reviews = (RecyclerView) findViewById(R.id.reviewList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
reviews.setLayoutManager(mLayoutManager);
rAdapter = new ReviewAdapter(MovieDetailActivity.this);
reviews.setAdapter(rAdapter);
You must create the adapter, Use this:
rAdapter = new ReviewAdapter(MovieDetailActivity.this);
reviews.setAdapter(rAdapter);
Instead
reviews.setAdapter(rAdapter);
rAdapter = new ReviewAdapter(MovieDetailActivity.this);
Remember, your Recycler view requires a LayoutManager, something like this:
reviews.setLayoutManager(
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
I am battling with the android Recyclerviewer. I followed most tutorials and also read posts here on StackOverflow, but i am still not coming right.
MainActivity
public class MainActivity extends ActionBarActivity {
RecyclerView myrecyclerView;
MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myrecyclerView = (RecyclerView)findViewById(R.id.myRecycleView);
adapter = new MyAdapter(MainActivity.this, getData());
final LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
myrecyclerView.setLayoutManager(layoutManager);
myrecyclerView.setAdapter(adapter);
myrecyclerView.setItemAnimator(new DefaultItemAnimator());
//
}
//the Dataset
public static List<MyDataModel> getData()
{
List<MyDataModel> myData = new ArrayList<>();
String[] postText = {"ibm", "apple", "google", "microsoft"};
int[] postImgUrl = {R.drawable.google, R.drawable.ibm, R.drawable.microsoft, R.drawable.apple};
for(int x=0; x<postText.length && x<postImgUrl.length; x++)
{
MyDataModel myModel = new MyDataModel();
myModel.postImgUrl = postImgUrl[x];
myModel.postText = postText[x];
myData.add(myModel);
}
return myData;
}
MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
List<MyDataModel> MyDataset;
private LayoutInflater inflater;
public MyAdapter(Context cont, List<MyDataModel> MyDataset)
{
inflater = LayoutInflater.from(cont);
}
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int ViewType)
{
//get ItemLayoutView from xml
View itemLayoutView = inflater.inflate(R.layout.my_items_row, parent, false);
//pass it to the ViewHolder
ViewHolder myViewHolder = new ViewHolder(itemLayoutView);
return myViewHolder;
}
public void onBindViewHolder(ViewHolder viewHolder, int position)
{
viewHolder.postText.setText(MyDataset.get(position).postText);
viewHolder.postImage.setImageResource(MyDataset.get(position).postImgUrl);
}
public int getItemCount()
{
return MyDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView postText;
public ImageView postImage;
public ViewHolder(View v)
{
super(v);
postText = (TextView)v.findViewById(R.id.myPostText);
postImage = (ImageView)v.findViewById(R.id.myPostImage);
}
}
}
My model Data
public class MyDataModel {
String postText;
int postImgUrl;
}
I am testing on real device through android studio, other apps i that i am also testing are fine.
Please help thank you.
You didn't initialize MyDataset inside MyAdapter class