RecyclerView not showing items. Adapter is not called - java

So I'm sure im making some trivial mistake somewhere and just can't see it. But I am simply trying to create a basic recyclerview list. However when I run the app nothing is shown, I put in log statements and found out that the adapter is created but nothing is ever called on it (like the bindviewholder, createviewholder, or getItemsize). I am unsure what is going on. Code is below"
MainActivity:
public class MainActivity extends GvrActivity {
private ArrayList<SampleItem> videos;
private RecyclerView recyclerView;
private SampleAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intialization();
}
private void intialization() {
Log.e("tree","mata intialized");
videos = new ArrayList<>();
videos.add(new SampleItem("http://vid1383.photobucket.com/albums/ah303/intellidev/congo_zpsrtmtey4l.mp4", "Adrian", 50, 100));
Log.e("tree","video added");
recyclerView = (RecyclerView) findViewById(R.id.recycle);
adapter = new SampleAdapter(this, videos);
recyclerView.setAdapter(adapter);
Log.e("tree","intialization completed");
}
}
Adapter
public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.ItemHolder> {
private Context context;
private ArrayList<SampleItem> items;
public SampleAdapter(Context context, ArrayList<SampleItem> videos) {
this.context = context;
this.items = videos;
Log.e("tree","adapter created");
}
#Override
public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.video_item, parent, false);
Log.e("tree","onCreateView Done");
return new ItemHolder(itemView);
}
#Override
public void onBindViewHolder(ItemHolder holder, int position) {
Uri uri = Uri.parse(items.get(0).getVideoUrl());
VrVideoView.Options options = new VrVideoView.Options();
options.inputType = VrVideoView.Options.TYPE_STEREO_OVER_UNDER;
try {
holder.view.loadVideo(uri, options);
holder.view.playVideo();
} catch (IOException e) {
Log.e("tree","onBindViewError " +e.getMessage());
}
holder.likes.setText(items.get(position).getNumOfLikes());
holder.shares.setText(items.get(position).getNumOfShares());
holder.profile.setText(items.get(position).getProfileName());
Log.e("tree","onBindViewDone");
}
#Override
public int getItemCount() {
Log.e("tree","items size is "+items.size());
return items.size();
}
class ItemHolder extends RecyclerView.ViewHolder {
private VrVideoView view;
private Button likes;
private Button shares;
private Button profile;
public ItemHolder(View itemView) {
// Stores the itemView in a public final member variable that can be used
// to access the context from any ViewHolder instance.
super(itemView);
view = (VrVideoView) itemView.findViewById(R.id.video_view);
likes = (Button) itemView.findViewById(R.id.likesButton);
shares = (Button) itemView.findViewById(R.id.sharesButton);
profile = (Button) itemView.findViewById(R.id.videoProfileButton);
}
}
}

If you want to use a RecyclerView, you will need to work with the following:
RecyclerView.Adapter - To handle the data collection and bind it to
the view
LayoutManager - Helps in positioning the items
ItemAnimator - Helps with animating the items for common operations
such as Addition or Removal of item
item animator is optional but adapter and Layoutmanager are necessary
you forgot to add layout manager. this two line of code probably solves your problem
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(llm);

Try this answer
https://stackoverflow.com/a/35802948/2144418
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

Add
recyclerView.setLayoutManager(new LinearLayoutManager(context));
after recycler view intialisation. You can also set GridLayoutManager if you want to use grid of items.

Related

RecyclerView Not Showing Items on Second Click

I have a RecyclerView that I am using to display a list that I obtain from making a request to remote api. The list is shown on the first time that the activity appears but when I go back and come back to activity again, the list doesn't show. How can I solve this?
This is the activity that I call the recyclerview.
public class DisplayMessage extends AppCompatActivity {
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.language_selection);
recyclerView = (RecyclerView) findViewById(R.id.recylerview);
LanguageAdapter languageAdapter = new LanguageAdapter(this);
recyclerView.setAdapter(languageAdapter);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
}
}
This is the Adapter class.
public class LanguageAdapter extends RecyclerView.Adapter<LanguageAdapter.MyViewHolder> {
ArrayList<Language> languageArrayList;
public LanguageAdapter(Context context) {
this.languageArrayList = new ArrayList<Language>();
RequestQueue queue = Volley.newRequestQueue(context);
String url ="https://api.bounswe2019group9.tk/contents/languages";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray data = (JSONArray) response.get("data");
for(int i=0; i<data.length();i++){
Language lang = new Language((String) data.get(i));
languageArrayList.add(lang);
Log.i("api", ""+lang.getLanguageName());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("language_selection", "Error on request to get language list");
}
});
queue.add(jsonObjectRequest);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.languages, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.languageName.setText(languageArrayList.get(position).getLanguageName());
}
#Override
public int getItemCount() {
return languageArrayList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView languageName;
public RelativeLayout relativeLayout;
public MyViewHolder(View itemView) {
super(itemView);
this.languageName = (TextView) itemView.findViewById(R.id.language);
this.relativeLayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
}
#Override
public void onClick(View v) {
}
}
}
OnCreate() method gets called only once when activity is created. You have set the adapter with the language list in OnCreate() and that it why it gets set only at the first time. When you reenter on the same screen then OnCreate won't get called again.
You have to set the adapter in the OnResume() method because this method will get called every time when you visit this screen.
In your Activity class, override an onResume method, and inside that also declare the following codes-
LanguageAdapter languageAdapter = new LanguageAdapter(this);
recyclerView.setAdapter(languageAdapter);
This is probably too late. But I had this problem as well. For me, the problem was my data binding. I used a custom delegation function to bind my views after they were inflated.
The problem was solved when I changed my delegation to inflate the view instead of binding the current inflated view.

(Android) Create RecyclerView with strings from list

I have a List<String> listOfNames that contains names. How to print them all using RecyclerView?
I was looking for a simple way of doing this and after some time of fiddling around, I think I have found a way.
Here I use an ArrayList of Strings.
Adapter Class
public class TagsAdapter extends RecyclerView.Adapter<TagsAdapter.TagsViewHolder> {
ArrayList<String> arrayList;
public TagsAdapter(ArrayList<String> arrayList) {
this.arrayList = arrayList;
}
#NonNull
#Override
public TagsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_holder_tags_chips, parent, false);
return new TagsAdapter.TagsViewHolder(layoutView);
}
#Override
public void onBindViewHolder(#NonNull final TagsViewHolder holder, final int position) {
holder.tagText.setText(arrayList.get(position));
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class TagsViewHolder extends RecyclerView.ViewHolder {
public TextView tagText;
public TagsViewHolder(View view) {
super(view);
tagText = view.findViewById(R.id.chipTextView);
}
}
}
Now in the Activity or fragment you want to show the recycler view, initialize your recycler view to the view in the xml
Tags.Java
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
In the same place add this method. I am assuming that the array list already has strings in it
private void getTags() {
recyclerView = view.findViewById(R.id.view);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(false);
layoutManager = new LinearLayoutManager(requireActivity(), LinearLayoutManager.HORIZONTAL, false);
//I have made my recycler view horizontal, if you want it vertical, just change the horizontal above to vertical.
recyclerView.setLayoutManager(layoutManager);
adapter = new TagsAdapter(tagsArray);
recyclerView.setAdapter(adapter);
}
I know this is an old question but might help someone who wants a simple and straightforward way of doing this.
Hi Simple example for recycler view.
Follow below steps:
Declare in global this variable
private RecyclerView recyclerview;
Use this where you get response from server and make one model class using GSON plugin just copy and paste your response.After use that.
Gson gson = new Gson();
CreateYourModel createYourModel = gson.fromJson(response_data, YourActivity.class);
RecyclerAdapter mAdapter = new RecyclerAdapter(createYourModel);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerview.setLayoutManager(mLayoutManager);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setAdapter(mAdapter);
recyclerview.setNestedScrollingEnabled(false);
This is your Recycler adapter just use same.
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private CreateYourModel mList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tvdate;
public MyViewHolder(View view) {
super(view);
tvdate = (TextView) view.findViewById(R.id.tvdate);
}
}
public RecyclerAdapterCreateYourModel mList) {
this.mList = mList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.youritemview, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tvsummuryloan_repayment_date.setText(setyourdatafromlist);
}
#Override
public int getItemCount() {
return mList.size();
}
}
You can follow any tutorial on google.
Thanks

Displaying recyclerView on MainActivity

I'm trying to display my recyclerView on MainActivity, but can't seem to do it.
This is my code: (which compiles with no errors)
private RecyclerView recyclerView;
private MyAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) this.findViewById(R.id.recycler_view_example);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyAdapter(this, getData());
recyclerView.setAdapter(adapter);
// Add Code to display recyclerView on Main...
}
// This is not my actual data, just testing it out
public static List<Block> getData() {
List<Block> data = new ArrayList<>();
String[] ids = {"310", "313", "320"};
String[] names = {"name of three ten", "name of three thirteen", "name of three twenty"};
for (int i=0; i<ids.length; i++) {
data.add(new Block(ids[i], names[i]));
}
return data;
}
And myActivity Class:
private final LayoutInflater inflater;
// Data: (information)
List<Block> data = new ArrayList<>();
public MyAdapter(Context context, List<Block> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.block, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
MyViewHolder holder = new MyViewHolder(h.itemView);
Block current = data.get(position);
holder.id.setText(current.getId());
holder.name.setText(current.getName());
}
#Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder {
TextView id;
TextView name;
Button button;
// Constructor
public MyViewHolder(View itemView) {
super(itemView);
id = (TextView) itemView.findViewById(R.id.the_course_id);
name = (TextView) itemView.findViewById(R.id.course_name);
button = (Button) itemView.findViewById(R.id.click);
}
}
}
All I'm trying to do now is that when I run the emulator, I will see the contents of the recyclerView. But, I've been stuck on this for a while as nothing seems to work.
Mind you I'm a beginner with Android, so forgive me if this is very trivial.
You should return the number of items in your list, within getItemCount as in:
#Override
public int getItemCount() {
return data.size();
}
Ok here you need to change the following: update -
recyclerView.setLayoutManager(new LinearLayoutManager(this));
to
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
and in adapter class change:
#Override
public int getItemCount() {
return data.size();
}
thats it. Happy coding :)
Change onBindViewHolder to:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
Block current = data.get(position);
h.id.setText(current.getId());
h.name.setText(current.getName());
}
also add #SelçukCihan solution to this
EDIT
Also if you want to use your MyViewHolder you have to change the following:
First change class signature:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
and also change the signature for onBindViewHolder:
public void onBindViewHolder(MyViewHolder h, int position)
and finally change the signature of your MyViewHolder class:
static class MyViewHolder extends RecyclerView.ViewHolder

Unable to Display List of Songs in RecyclerView

I am using a recyclerView to display a list of songs but am unable to do so. Kindly refer to the code and kindly tell me where am i mistaken. Thankyou.
SongsFragment:-
public class Fragment_Songs extends android.support.v4.app.Fragment {
/**
* Big list with all the Songs found.
*/
public ArrayList<Song> songs = new ArrayList<Song>();
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
public static Fragment_Songs newInstance() {
return new Fragment_Songs();
}
/**
* Returns a new list with all songs.
*
* #note This is different than accessing `songs` directly
* because it duplicates it - you can then mess with
* it without worrying about changing the original.
*/
public ArrayList<Song> getSongs() {
ArrayList<Song> songList = new ArrayList<Song>();
for (Song song : songs)
songList.add(song);
return songList;
}
public Fragment_Songs() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_songs, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mRecyclerView = (RecyclerView) view.findViewById(R.id.songs_recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new RecyclerViewMaterialAdapter(new Songs_Adapter(getActivity(), getSongs()));
mRecyclerView.setAdapter(mAdapter);
MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}
}
and here is my recyclerView adapter:-
public class Songs_Adapter extends RecyclerView.Adapter<Songs_Adapter.ViewHolder> {
private ArrayList<Song> songs = new ArrayList<Song>();
public Songs_Adapter(Context c, ArrayList<Song> theSongs) {
songs = theSongs;
}
#Override
public int getItemCount() {
return songs.size();
}
#Override
public Songs_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Song currentSong = songs.get(position);
holder.trackName.setText(currentSong.getArtist());
holder.albumInfo.setText(currentSong.getAlbum());
holder.yearInfo.setText(currentSong.getYear());
holder.songDuration.setText((int) currentSong.getDurationMinutes());
}
public class ViewHolder extends RecyclerView.ViewHolder {
CardView cv;
ImageView albumArt;
TextView trackName;
TextView albumInfo;
TextView yearInfo;
TextView songDuration;
public ViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.list_item_card);
albumArt = (ImageView)itemView.findViewById(R.id.albumArtImage);
trackName = (TextView)itemView.findViewById(R.id.trackName);
albumInfo = (TextView)itemView.findViewById(R.id.albumData);
yearInfo = (TextView)itemView.findViewById(R.id.yearInfo);
songDuration = (TextView)itemView.findViewById(R.id.songDuration);
}
}
public ArrayList<Song> getSongs() {
return songs;
}
}
is my arrayList code correct? I couldnot get it working if someone would help? . Thank you.
At first glance it looks like your not using the correct constructor in your adapter.
You are using
mAdapter = new RecyclerViewMaterialAdapter(new Songs_Adapter());
You should be using
mAdapter = new RecyclerViewMaterialAdapter(new Songs_Adapter(getActivity(), songs));
You are creating the songs ArrayList but never passing it to the your adapter. Then how will your adapter get the data? Do something like this -
mAdapter = new RecyclerViewMaterialAdapter(new Songs_Adapter(getActivity(), songs));
mRecyclerView.setAdapter(mAdapter);
This way your songs ArrayList will be passed to the adapter and can be used to populate the RecyclerView.

android recyclerview doesn't display items

I want to show these items inside my recyclerview but it doesn't show at all and I can't see the error. Maybe you guys can help me out.
MainActivity.java
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rec);
List<MenuData> list = new ArrayList<>();
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
MenuRecAdapter menuRecAdapter = new MenuRecAdapter(list);
recyclerView.setAdapter(menuRecAdapter);
RecyclerView adapter:
public class MenuRecAdapter extends RecyclerView.Adapter<RecViewHolder>{
private List<MenuData> mList;
Activity context;
public MenuRecAdapter(List<MenuData> mList){
this.mList = mList;
}
public int getItemCount(){
return mList.size();
}
public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
RecViewHolder pvh = new RecViewHolder(v);
return pvh;
}
public void onBindViewHolder(RecViewHolder holder, int i){
holder.menuTeXT.setText(mList.get(i).text);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
ViewHolder of the items:
public class RecViewHolder extends RecyclerView.ViewHolder {
public TextView menuTeXT;
public RecViewHolder(View itemView){
super(itemView);
menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT);
}
}
and the data I want to put into my recyclerview (what doesn't show):
class MenuData {
String text;
MenuData(String text){
this.text = text;
}
private List<MenuData> list;
private void initializeData(){
list = new ArrayList<>();
list.add(new MenuData("Featured"));
list.add(new MenuData("Categories"));
list.add(new MenuData("Sell"));
list.add(new MenuData("Settings"));
list.add(new MenuData("Logout"));
}
}
Thanks in advance
In MainActivity
ArrayList<String> list = new ArrayList<>();
list.add("something1");
list.add("something2");
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rec);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
MenuRecAdapter menuRecAdapter = new MenuRecAdapter(list);
recyclerView.setAdapter(menuRecAdapter);
RecyclerView Adapter
public class MenuRecAdapter extends RecyclerView.Adapter<RecViewHolder>{
private ArrayList<String> mList = new ArrayList<>();
Activity context;
public MenuRecAdapter(ArrayList<String> mList){
this.mList = mList;
}
public int getItemCount(){
return mList.size();
}
public RecViewHolder onCreateViewHolder(ViewGroup viewGroup, int position){
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.menuitem, viewGroup, false);
RecViewHolder pvh = new RecViewHolder(v);
return pvh;
}
public void onBindViewHolder(RecViewHolder holder, int i){
holder.menuTeXT.setText(mList.get(i));
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
and ViewHolder remains same...
public class RecViewHolder extends RecyclerView.ViewHolder {
public TextView menuTeXT;
public RecViewHolder(View itemView){
super(itemView);
menuTeXT = (TextView)itemView.findViewById(R.id.menuTXT);
}
}
also get rid of MenuData class. The above code should work fine.
As it was mentioned above in comments, the problem might be in non-specifying the layoutManager attribute of the RecyclerView.
The layoutManager can be specified either in XML-file or dynamically in Java code.
Example of Java code from the answer above:
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
To add layoutManager via XML use the appropriate attribute:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
This fixed problem for me.
Since you are not getting any data, I guess the problem happens at the time you pass the list into your adapter. I see you didn't make copy of your list, so you are passing the reference of the list directly into the adapter. I suggest you to try
MenuRecAdapter menuRecAdapter = new MenuRecAdapter(new Arraylist(list));

Categories