I want to display my recyclerview list in a fragment. There are no data shows when I run the app. Here's my Moviesfragment:
public class MoviesFragment extends Fragment {
private RecyclerView rvMovies;
private ArrayList<Movies> list = new ArrayList<>();
public MoviesFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_movies, container, false);
rvMovies = (RecyclerView) view.findViewById(R.id.rv_movies);
rvMovies.setHasFixedSize(true);
list.addAll(getListMovie());
showRecyclerList();
return view;
}
public ArrayList<Movies> getListMovie(){
String [] dataTitles = getResources().getStringArray(R.array.MovieTitle);
String [] dataReleases = getResources().getStringArray(R.array.MovieReleaseInfo);
String [] dataDirectors = getResources().getStringArray(R.array.MovieDirectors);
String [] dataGenres = getResources().getStringArray(R.array.MovieGenres);
String [] dataPhoto = getResources().getStringArray(R.array.MovieImage);
ArrayList<Movies> listMovie = new ArrayList<>();
for (int i = 0 ; i < dataTitles.length; i++){
Movies movies = new Movies();
movies.setPhoto(dataPhoto[i]);
movies.setTitle(dataTitles[i]);
movies.setRelease_info(dataReleases[i]);
movies.setDirector(dataDirectors[i]);
movies.setGenres(dataGenres[i]);
listMovie.add(movies);
}
return listMovie;
}
private void showRecyclerList(){
rvMovies.setLayoutManager(new LinearLayoutManager(this.getContext()));
ListMoviesAdapter listMoviesAdapter = new ListMoviesAdapter(list);
rvMovies.setAdapter(listMoviesAdapter);
}
}
Here is my adapter:
public class ListMoviesAdapter extends RecyclerView.Adapter<ListMoviesAdapter.ListViewHolder> {
private ArrayList<Movies> listMovies;
public ListMoviesAdapter(ArrayList<Movies> list){
this.listMovies = list;
}
public void setListMovies(ArrayList<Movies> list){
this.listMovies = list;
}
@NonNull
@Override
public ListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_movies, parent, false);
return new ListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ListViewHolder holder, int position) {
Movies movies = listMovies.get(position);
Glide.with(holder.itemView.getContext())
.load(movies.getPhoto())
.apply(new RequestOptions().override(55,55))
.into(holder.imgPhoto);
holder.tvTitle.setText(movies.getTitle());
holder.tvRelease.setText(movies.getRelease_info());
holder.tvDirector.setText(movies.getDirector());
holder.tvGenre.setText(movies.getGenres());
}
@Override
public int getItemCount() {
return 0;
}
public class ListViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvTitle, tvRelease, tvDirector, tvGenre;
public ListViewHolder(@NonNull View itemView) {
super(itemView);
imgPhoto = itemView.findViewById(R.id.image_movies);
tvTitle = itemView.findViewById(R.id.tv_item_movies_name);
tvRelease = itemView.findViewById(R.id.tv_item_movies_release);
tvDirector = itemView.findViewById(R.id.tv_item_movies_directors);
tvGenre = itemView.findViewById(R.id.tv_item_movies_genres);
}
}
}
Movie.java:
private String title;
private String release_info;
private String genres;
private String director;
private String photo;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getRelease_info() {
return release_info;
}
public void setRelease_info(String release_info) {
this.release_info = release_info;
}
public String getGenres() {
return genres;
}
public void setGenres(String genres) {
this.genres = genres;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.title);
dest.writeString(this.release_info);
dest.writeString(this.director);
dest.writeString(this.genres);
dest.writeString(this.photo);
}
Movies(){}
private Movies(Parcel in){
this.title = in.readString();
this.release_info = in.readString();
this.director = in.readString();
this.genres = in.readString();
this.photo = in.readString();
}
public static final Parcelable.Creator<Movies> CREATOR = new Parcelable.Creator<Movies>(){
@Override
public Movies createFromParcel(Parcel source){
return new Movies(source);
}
@Override
public Movies[] newArray(int size){
return new Movies[size];
}
};
}
Here's my item_row_movies:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<ImageView
android:id="@+id/image_movies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/image_movies"
android:layout_toRightOf="@+id/image_movies"
android:orientation="vertical">
<TextView
android:id="@+id/tv_item_movies_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv_item_movies_release"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textSize="8sp"/>
<TextView
android:id="@+id/tv_item_movies_directors"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textSize="8sp"/>
<TextView
android:id="@+id/tv_item_movies_genres"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:textSize="8sp"/>
</LinearLayout>
</RelativeLayout>
And my fragment movies layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MoviesFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/section_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:text="@string/Movies_Tab_title" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_movies"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_row_movies"/>
</FrameLayout>
I already tried to change from this() in layoutmanager to this.getContext() since it's not an activity. There is no error shown, but the list didn't show.
set layout manager for list after setting adapter