问题
I have this code and everytime I tried to run it ..the application is not running not error but not running can anyone please help me to determine what is the reason for getting this kind of problem.
because I'm doing RecyclerView with CardView in fragment I have this problem almost a week :(
this is my code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext ;
private List<Book> mData ;
public RecyclerViewAdapter(Context mContext, List<Book> mData) {
this.mContext = mContext;
this.mData = mData;
}
public RecyclerViewAdapter(TimeFragment timeFragment, List<Book> lstBook) {
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view ;
LayoutInflater mInflater = LayoutInflater.from(mContext);
view = mInflater.inflate(R.layout.cardveiw_item_book,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_book_title.setText(mData.get(position).getTitle());
holder.img_book_thumbnail.setImageResource(mData.get(position).getThumbnail());
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tv_book_title;
public ImageView img_book_thumbnail;
CardView cardView ;
public MyViewHolder(View itemView) {
super(itemView);
tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id) ;
img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_img_id);
cardView = (CardView) itemView.findViewById(R.id.cardview_id);
}
}
}
Here's is the related POJO class.
public class Book {
private String Title;
private String Category ;
private String Description ;
private int Thumbnail ;
public Book() {
}
public Book(String title, String category, String description, int thumbnail) {
Title = title;
Category = category;
Description = description;
Thumbnail = thumbnail;
}
public String getTitle() {
return Title;
}
public String getCategory() {
return Category;
}
public String getDescription() {
return Description;
}
public int getThumbnail() {
return Thumbnail;
}
public void setTitle(String title) {
Title = title;
}
public void setCategory(String category) {
Category = category;
}
public void setDescription(String description) {
Description = description;
}
public void setThumbnail(int thumbnail) {
Thumbnail = thumbnail;
}
}
Code for the fragment that sets the adapter to RecyclerView.
public class TimeFragment extends Fragment {
List<Book> lstBook ;
public TimeFragment() {
}
@Nullable
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lstBook = new ArrayList<>();
lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.ic_in));
lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.ic_out));
lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.ic_in));
lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.ic_out));
}
@Nullable
@Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view;
view = inflater.inflate( R.layout.recyclerview, container, false);
RecyclerView myrv = (RecyclerView) view.findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,lstBook);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
myrv.setLayoutManager(gridLayoutManager);
myrv.setAdapter(myAdapter);
return view;
}
}
回答1:
There are actually two ways to do it.
1. Remove the below-mentioned line from the RecyclerViewAdapter.
public RecyclerViewAdapter(TimeFragment timeFragment, List lstBook) { }
and in the fragment code, there is also a minor change kindly look the code below and also I didn't have the drawable so I used the default one.
public class TimeFragment extends Fragment {
List<Book> lstBook ;
public TimeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lstBook = new ArrayList<>();
lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.ic_launcher_background));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_time, container, false);
RecyclerView myrv = (RecyclerView) view.findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(getContext(),lstBook);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
myrv.setLayoutManager(gridLayoutManager);
myrv.setAdapter(myAdapter);
return view;
}
}
And if you don't remove the above-mentioned line still it is going to work. Just modify the code of your TimeFragment.
What has happened is when you have the context in the adapter constructor you have actually used the second constructor of the adapter which takes the argument as shown below
public RecyclerViewAdapter(TimeFragment timeFragment, List lstBook) {
}
In this, you have actually not populated the list, which you have done in the first constructor shown below.
public RecyclerViewAdapter(Context mContext, List<Book> mData) {
this.mContext = mContext;
this.mData = mData;
}
Now the app crashes because when it finds the list in the adapter it is always empty. so it crashes. I have changed a few lines in the code in both the adapter part and the fragment part. Kindly find the changes below.
RecyclerViewAdapter:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Context mContext ;
private List<Book> mData ;
private TimeFragment timeFragment;
public RecyclerViewAdapter(Context mContext, List<Book> mData) {
this.mContext = mContext;
this.mData = mData;
}
public RecyclerViewAdapter(TimeFragment timeFragment, List<Book> lstBook) {
this.timeFragment = timeFragment;
this.mData = lstBook;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view ;
LayoutInflater mInflater = LayoutInflater.from(timeFragment.getContext());
view = mInflater.inflate(R.layout.cardveiw_item_book,parent,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
holder.tv_book_title.setText(mData.get(position).getTitle());
holder.img_book_thumbnail.setImageResource(mData.get(position).getThumbnail());
}
@Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tv_book_title;
public ImageView img_book_thumbnail;
CardView cardView ;
public MyViewHolder(View itemView) {
super(itemView);
tv_book_title = (TextView) itemView.findViewById(R.id.book_title_id) ;
img_book_thumbnail = (ImageView) itemView.findViewById(R.id.book_img_id);
cardView = (CardView) itemView.findViewById(R.id.cardview_id);
}
}
}
Time Fragment:
public class TimeFragment extends Fragment {
List<Book> lstBook ;
public TimeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TestFailed","OnCreate");
lstBook = new ArrayList<>();
lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.ic_launcher_background));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Log.d("TestFailed","OnCreateView");
View view = inflater.inflate(R.layout.fragment_time, container, false);
RecyclerView myrv = (RecyclerView) view.findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,lstBook);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
myrv.setLayoutManager(gridLayoutManager);
myrv.setAdapter(myAdapter);
return view;
}
}
Hope that helps and clears the doubt.
回答2:
No context is passed from your fragment. You are passing this which is TimeFragment so the constructor being invoked is
public RecyclerViewAdapter(TimeFragment timeFragment, List<Book> lstBook)
Use the activity context instead.
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(getActivity(),lstBook);
回答3:
Replace Three things in your code as follows:
From
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(this,lstBook);
To
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(getActivity(),lstBook);
From
public class RecyclerViewAdapter extends RecyclerView.Adapter {
To
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
From (Replace from tow consturcter to one as Bellow)
public RecyclerViewAdapter(Context mContext, List<Book> mData) {
this.mContext = mContext;
this.mData = mData;
}
public RecyclerViewAdapter(TimeFragment timeFragment, List<Book> lstBook) {
}
To
public RecyclerViewAdapter(Context mContext, List<Book> mData) {
this.mContext = mContext;
this.mData = mData;
}
回答4:
public class TimeFragment extends Fragment {
List<Book> lstBook ;
public TimeFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lstBook = new ArrayList<>();
lstBook.add(new Book("The Vegitarian","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("The Wild Robot","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("Maria Semples","Categorie Book","Description book",R.drawable.ic_launcher_background));
lstBook.add(new Book("The Martian","Categorie Book","Description book",R.drawable.ic_launcher_background));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_time, container, false);
RecyclerView myrv = (RecyclerView) view.findViewById(R.id.recyclerview_id);
RecyclerViewAdapter myAdapter = new RecyclerViewAdapter(getContext(),lstBook);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
myrv.setLayoutManager(gridLayoutManager);
myrv.setAdapter(myAdapter);
return view;
}
}
来源:https://stackoverflow.com/questions/50558081/how-to-run-my-recyclerview-with-cardview-in-fragment