RecyclerView items are not getting created - I'm trying to add a note feature (with MVVM+Room+liveData) to my project.
The same code is fine when used in a new android studio project. implementing it in my existing project is the issue. (both are with androidX and have the same implementations).
Other recyclerViews in my app are working correctly (not with MVVM)
The recycler view adapter is not getting accessed - I used Log flags.
The Code source is from here: https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-6-recyclerview-adapter
Note activity:
public class NoteActivity extends AppCompatActivity {
private NoteViewModel noteViewModel;
ArrayList<Reviews> reviewList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final NoteAdapter adapter = new NoteAdapter();
recyclerView.setAdapter(adapter);
noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
#Override
public void onChanged(#Nullable List<Note> notes) {
adapter.setNotes(notes);
}
});
}
Note Adapter:
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
private static final String TAG = "Debug "+NoteAdapter.class.getSimpleName();
private List<Note> notes = new ArrayList<>();
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_item, parent, false);
Log.e (TAG, "in onCreateViewHolder");
return new NoteHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull NoteHolder holder, int position) {
Note currentNote = notes.get(position);
holder.textViewTitle.setText(currentNote.getTitle());
holder.textViewDescription.setText(currentNote.getDescription());
holder.textViewPriority.setText(String.valueOf(currentNote.getPriority()));
Log.e (TAG, "in onBindViewHolder");
}
#Override
public int getItemCount() {
return notes.size();
}
public void setNotes(List<Note> notes) {
this.notes = notes;
notifyDataSetChanged();
}
class NoteHolder extends RecyclerView.ViewHolder {
private TextView textViewTitle;
private TextView textViewDescription;
private TextView textViewPriority;
public NoteHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_description);
textViewPriority = itemView.findViewById(R.id.text_view_priority);
Log.e (TAG, "in NoteHolder");
}
}
}
the set notes void is the only function getting accessed. (using Log Flags)
The entire code with MVVM classes works in a blank studio project.
What could be the problem??
in Log it get this error:
E/RecyclerView: No adapter attached; skipping layout
The issue is resolved after logging in MVVM components.
I removed the application from my device and installed it again, so the Room database instance was recreated correctly. Apparently, The instance was empty (I don't know why).
btw,
this log error is not part of the issue (you can get it when everything is okay) see previously answered question in the link below
E/RecyclerView: No adapter attached; skipping layout
recyclerview No adapter attached; skipping layout
Related
I have a fragment Users which has 3 other fragments in it (tabs). For one tab ( called Friends2Fragment ) I made a recycler View and made an adapter for it. In each item of RecyclerView I have a button "Add friend" and I want to call it from Friends2Fragment, not to call it from the adapter because I can't use Firestore Database properly.
RecyclerViewInterface:
public interface RecyclerViewInterface {
void onItemClick(int position, String button_pressed);
}
Friends2Fragment.java :
public void onStart(){
super.onStart();
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
friendslist = new ArrayList<>();
myAdapter = new MyAdapter(friendslist,v.getContext());
recyclerView.setAdapter(myAdapter);
------ Firestore operations ------
}
#Override
public void onItemClick(int position, String button_pressed) {
switch ( button_pressed ){
case "ADD_FRIEND":
Log.d(TAG, "item clicked: " + friendslist.get(position).username);
}
}
MyAdapter.java :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
Context context;
public ArrayList<User> userArrayList;
public MyAdapter(ArrayList<User> userArrayList, Context context) {
this.userArrayList = userArrayList;
this.context = context;
}
public Context getContext() {
return context;
}
public ArrayList<User> getUserArrayList() {
return userArrayList;
}
#NonNull
#Override
public MyAdapter.myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
MyAdapter.myViewHolder myViewHolder = new MyAdapter.myViewHolder(v);
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((Friends2Fragment)context).onItemClick(myViewHolder.getAdapterPosition(),"ADD_FRIEND");
}
});
return myViewHolder;
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.myViewHolder holder, int position) {
User user = userArrayList.get(position);
holder.usernamerecycle.setText(user.username);
}
#Override
public int getItemCount() {
return userArrayList.size();
}
public void filterList(List<User> filteredList){
userArrayList = (ArrayList<User>) filteredList;
notifyDataSetChanged();
}
public class myViewHolder extends RecyclerView.ViewHolder{
TextView usernamerecycle;
Button addbutton;
View rootview;
public myViewHolder(#NonNull View itemView) {
super(itemView);
rootview = itemView;
usernamerecycle = itemView.findViewById(R.id.usernamerecycler);
addbutton = itemView.findViewById(R.id.addfriendbutton);
}
}
}
The problem is at this line : ((Friends2Fragment)context).onItemClick(myViewHolder.getAdapterPosition(),"ADD_FRIEND"); in onCreateViewHolder method in MyAdapter.
I have this error : Inconvertible types; cannot cast 'android.content.Context' to 'com.example.birthday.Fragments.Friends2Fragment'
Please help me ..
A Fragment isn't a Context (that's not one of its supertypes) so that cast is impossible, that's why you're getting the error.
I think you should organise it like this: your Adapter holds a bunch of User objects, right? It displays those, and you have a click listener on each ViewHolder that knows which index in the User list it's currently displaying, and it wants to inform some listener when it's clicked. That index is an internal detail really, it would make more sense to look up the actual User, and provide that to the listener.
The simplest way is to just provide your fragment as a listener. First store it in your adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
// store a reference to your fragment
private Friends2Fragment listener;
// add a function to provide that fragment
public void setListener(Friends2Fragment: listener) {
this.listener = listener
}
...
public MyAdapter.myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
...
myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null) {
// look up the actual user
User user = userArrayList.get(myViewHolder.getAdapterPosition());
// call a function on your fragment
listener.onItemClick(user, "ADD_FRIEND");
}
}
});
}
Then add the callback function your adapter uses, and also set your fragment on the adapter as a listener:
// Friends2Fragment
// You should REALLY be doing this in onViewCreated or something, so this setup happens once.
// You're losing all your state by creating a new adapter whenever the user returns to the app
public void onStart(){
...
myAdapter = new MyAdapter(friendslist,v.getContext());
// set the fragment as the listener
myAdapter.setListener(this);
recyclerView.setAdapter(myAdapter);
}
// now add the function the adapter calls
private void onItemClick(User user, String someString) {
// handle the clicked user
}
A better way is to create an interface with all the events that need to be handled, and make your Fragment implement those. It breaks the hard association with the Fragment since you could pass any object that implements those functions, and it's also clearer because the interface kinda documents all the data the adapter produces, and that a listener needs to be able to handle. Something like this:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {
// the listener is now something that implements the Callbacks interface
private Callbacks listener;
...
// nesting it inside MyAdapter makes the path MyAdapter.Callbacks, which makes it clear
// exactly what it is and what it relates to, and kinda gives the Adapter "ownership"
interface Callbacks {
void addFriend(User user)
}
And then you just make the Fragment implement that interface
public class Friends2Fragment() extends Fragment implements MyAdapter.Callbacks {
...
// implement all the callbacks you need to handle
override public void addFriend(User user) {
// do the thing
}
// set it in the same way, since this Fragment implements MyAdapter.Callbacks
myAdapter.setListener(this);
Which is a bit neater and cleaner, I think - but slightly more work. Also if you notice, I renamed the callback function from the generic handleItemClick to the more specific addFriend - so instead of having to pass a String saying what kind of click it is, you just have a function for each event you want to handle, and you can name them appropriately
hi i am trying use recyclerview. I have two layout one of them mainactivity which has cardview and recyclerview and other one is feeditem xml this one has hold item requirements for show in recyclerview(name,feed,date). But i guess i might have problem onBindViewHolder or VievHolder because i just see mainactivity i cant show anything in recyclerview and the tools inside the feeditem xml actually i can't bind each other.
MainActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.activity_main);
recyclerView = this.findViewById(R.id.rv_f2);
// recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
databaseReference = database.getReference("allfeed");
FirebaseRecyclerOptions<feedmember> options = new FirebaseRecyclerOptions.Builder<feedmember>().setQuery(databaseReference, feedmember.class).build();
FirebaseRecyclerAdapter<feedmember, VievHolder_feed> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<feedmember, VievHolder_feed>(options) {
#Override
protected void onBindViewHolder(#NonNull VievHolder_feed holder, int position, #NonNull feedmember model)
{ holder.setitem(this , model.getName(), model.getFeed(), model.getTime()); }
#NonNull
#Override
public VievHolder_feed onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_item, parent, false);
return new VievHolder_feed(view); }};
firebaseRecyclerAdapter.startListening();
recyclerView.setAdapter(firebaseRecyclerAdapter); }}
Viewholder_feed
public class VievHolder_feed extends RecyclerView.ViewHolder {
TextView time_result,name_result,que_result;
public VievHolder_feed(#NonNull View itemView) { super(itemView); }
public void setitem(FirebaseRecyclerAdapter<feedmember, VievHolder_feed> activity, String name, String time, String feed){
time_result =itemView.findViewById(R.id.time_que_item);
name_result =itemView.findViewById(R.id.name_que_item_tv);
que_result =itemView.findViewById(R.id.que_item_tv);
time_result.setText(time);
name_result.setText( name);
que_result.setText(feed); }}
[]
It seems that your recycler view received the item, but the view holder binds the fields with blank contents.
Did you verify if the mapping inside feedmember class works fine ?
I am making an application that takes all users of a table in Firebase and shows them in CardView, but I would like administrators to appear before the other users.
For this differentiation, all users were saved to the system with an attribute called type which says whether they are normal users or administrators.
This is TelaChat.java file:
public class TelaChat extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference databaseReference;
private FirebaseRecyclerOptions<Usuario> options;
private FirebaseRecyclerAdapter<Usuario, ChatAdapter> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
setTitle("Tela de Chat");
recyclerView = findViewById(R.id.cm_rv_membros);
recyclerView.setHasFixedSize(true);
databaseReference = FirebaseDatabase.getInstance().getReference().child("usuario");
options = new FirebaseRecyclerOptions.Builder<Usuario>().setQuery(databaseReference, Usuario.class).build();
adapter = new FirebaseRecyclerAdapter<Usuario, ChatAdapter>(options) {
#Override
protected void onBindViewHolder(ChatAdapter holder, int position, Usuario model) {
Picasso.with(holder.ivFoto.getContext()).load(model.getImageURL()).centerCrop().resize(140, 140).transform(new ImageTrans_CircleTransform2()).into(holder.ivFoto);
holder.tvNome.setText(model.getNome());
holder.tvTipo.setText(model.getTipo());
}
#Override
public ChatAdapter onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_card, parent, false);
return new ChatAdapter(view);
}
};
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 1);
recyclerView.setLayoutManager(gridLayoutManager);
adapter.startListening();
recyclerView.setAdapter(adapter);
}
This is ChatAdapter.java file:
public class ChatAdapter extends RecyclerView.ViewHolder{
public ImageView ivFoto;
public TextView tvNome;
public TextView tvTipo;
public ChatAdapter(View itemView) {
super(itemView);
ivFoto = itemView.findViewById(R.id.cc_iv_foto);
tvNome = itemView.findViewById(R.id.cc_tv_nome);
tvTipo = itemView.findViewById(R.id.cc_tv_tipo);
}
}
I've looked for some tutorials on the internet but none has specified what I want to do. Can someone help me?
Try to add this: orderByChild("type")
databaseReference = FirebaseDatabase.getInstance().getReference().child("usuario").orderByChild("type");
Or here:
options = new FirebaseRecyclerOptions.Builder<Usuario>().setQuery(databaseReference.orderByChild("type"), Usuario.class).build();
Sorry I can't test it right now, hope it works.
I have an idea may help in this set a flag for example called position with initial value 0 and create a new list to order on it and check the type and if is an admin add him in the list with position value and increase the position value else add the value without index
private int position = 0;
private List<Users> myList = new ArrayList();
for(int i = 0 ; i < usersList.size ; i++){
if(userList.get(i).getType.equal("adminstrator")){
myList.add(position,userList.get(i))
position++;
}else{
myList.add(usetList.get(i))
}
}
after set this list "myList" to the recyclerview adapter
I have implemented RecyclerView show some realm table data. I've created adapter by extending from RealmRecyclerViewAdapter. Even thought I have 3 rows in table (Which I checked by result debugging) nothing showing up in the list. I'm using 'io.realm:android-adapters:2.1.0' adapter and Realm 3.7.2
Here's my code.
Adapter class.
public class UserListAdapter extends RealmRecyclerViewAdapter<User,UserListAdapter.ViewHolder> {
public UserListAdapter(#Nullable OrderedRealmCollection<User> data, boolean autoUpdate) {
super(data, autoUpdate);
setHasStableIds(true);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.user_list_layout, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
User user = getItem(position);
holder.home.setText(user.getUid());
holder.name.setText(user.getName());
holder.home.setText(user.getHouse().getName());
if(user.isStatus()) {
holder.status.setText("Active");
}else{
holder.status.setText("Inactive");
}
}
#Override
public long getItemId(int index) {
return getItem(index).getUid();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
#BindView(R.id.column_id)
TextView id;
#BindView(R.id.column_name)
TextView name;
#BindView(R.id.column_house)
TextView home;
#BindView(R.id.status_button)
Button status;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
itemView.setOnClickListener(this);
status.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.status){
//TODO: create interface and add method tho trigger when status click
}else{
//TODO: create interface and add method tho trigger when item click
}
}
}
}
code fragment for setting adapter
try(Realm realm = Realm.getDefaultInstance()){
RealmResults<User> users = realm.where(User.class).findAll();
listAdapter = new UserListAdapter(users,true);
recyclerView.setAdapter(listAdapter);
recyclerView.addItemDecoration(new RecyclerViewDividerVertical(2));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
}
I could have conver RealmResults<User> in to ArrayList<User> and implement normal RecyclerView Adapter and get it done. But As I've understand RealmRecyclerViewAdapter will result auto update result data in case of data change. But i'm not 100% clear on that too. Can any one explain how that works ? Thanks.
try(Realm realm = Realm.getDefaultInstance()){
RealmResults<User> users = realm.where(User.class).findAll();
listAdapter = new UserListAdapter(users,true);
recyclerView.setAdapter(listAdapter);
recyclerView.addItemDecoration(new RecyclerViewDividerVertical(2));
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
} // <--- auto-close!
If you close the Realm instance then the results bound to it become invalidated, and RealmRecyclerViewAdapter checks against isValid() which will return false because you've closed the Realm, so it will be shown with 0 element count.
Refer to proper lifecycle management for UI thread Realm instance (onCreate() open, onDestroy() close) in the documentation.
Looks like your data is invalid and getItemCount() method which is implemented in RealmRecyclerViewAdapter returns 0.
I am developing an android application where i am using a RecyclerView to display a list of items.I am getting the list from server as json.So my problem is within this list i am getting another list as item.That is if my main arraylist contain title and materials, the material is another arraylist.So can you please suggest a solution to display a list within recyclerview.
The code below is my adapter
public class CurriculumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<Curriculum> mArrayListCurriculum;
public CurriculumAdapter(Context mContext, ArrayList<Curriculum> mArrayListCurriculum) {
this.mContext = mContext;
this.mArrayListCurriculum = mArrayListCurriculum;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_key_features, parent,false);
return new KeyFeatureViewHolder(v);
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof KeyFeatureViewHolder) {
((KeyFeatureViewHolder) holder).mTextViewFeatureTitle.setText(mArrayListCurriculum.get(position).getTitle());
}
}
#Override
public int getItemCount() {
return mArrayListCurriculum == null ? 0 : mArrayListCurriculum.size();
}
public static class KeyFeatureViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewFeatureTitle;
public KeyFeatureViewHolder(View itemView) {
super(itemView);
mTextViewFeatureTitle = (TextView) itemView.findViewById(R.id.txtFeature);
}
}
}
The code below is my fragment with dummy arraylist data
public class CourseCurriculumFragment extends Fragment {
private FragmentInterface mFragmentInterface;
private ArrayList<Curriculum> mArrayListCurriculum;
private ArrayList<Material> mArrayListMaterial;
private RecyclerView mRecyclerViewCurriculum;
private LinearLayoutManager mLinearLayoutManager;
private CurriculumAdapter mCurriculumAdapter;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_course_curriculum, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView(view);
}
private void initView(View view) {
mArrayListMaterial = new ArrayList<>();
mArrayListCurriculum = new ArrayList<>();
populateMaterials();
populateKeyFeatures();
mRecyclerViewCurriculum = (RecyclerView) view.findViewById(R.id.recyclerViewCurriculum);
mCurriculumAdapter = new CurriculumAdapter(getActivity(), mArrayListCurriculum);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerViewCurriculum.setLayoutManager(mLinearLayoutManager);
mRecyclerViewCurriculum.setAdapter(mCurriculumAdapter);
mRecyclerViewCurriculum.setItemAnimator(new DefaultItemAnimator());
}
private void populateMaterials() {
mArrayListMaterial.add(new Material("12:00","pdf","","Sample Text","0"));
mArrayListMaterial.add(new Material("12:00","pdf","","Sample Text","0"));
}
private void populateKeyFeatures() {
mArrayListCurriculum.add(new Curriculum("UNIT 1",mArrayListMaterial));
mArrayListCurriculum.add(new Curriculum("UNIT 2",mArrayListMaterial));
mArrayListCurriculum.add(new Curriculum("UNIT 3",mArrayListMaterial));
}
}
A bind method in a holder is a good way to pass data to it.
In your case this bind method should take in a Curriculum and a Material object as parameters.
Inside the onBindViewHolder method of the adapter, instead of reaching into the variables of the holder, you should call this bind method.
In the implementation of the method inside the you KeyFeatureViewHolder class you should use these passed parameters and display them in the appropriate UI elements.
Lastly, to get the Material object data into adapter, add ArrayList<Material> as a constructor parameter just like you did with Curriculum.
Use RecyclerView with header, title as header and materials as items of that header. Look at this example.
You need to design a custom list for yourself. For example take an object like this.
public class ListItem {
public curriculumName = null;
public materialName = null;
}
Now populate this list after you parse the JSON string. Get your first Curriculum and populate the object like this
private ArrayList<ListItem> mListItemArray = new ArrayList<ListItem> ();
for(curriculum : mArrayListCurriculum) {
ListItem mListItemHead = new ListItem();
mListItemHead.curriculumName = curriculum.getName();
// Set the header here
mListItemArray.add(mListItemHead);
for(material : curriculum.getMaterials()){
ListItem mListItem = new ListItem();
mListItem.materialName = material.getName();
// Add materials here
mListItemArray.add(mListItem);
}
}
Now, you've a list with headers and materials. When the materialName in your mListItemArray is null, it identifies that this is a header and vice versa.
Now the trick is to modify your adapter of your RecyclerView so that you can bind proper view to your items in your list.
You can find an indication from this answer on how you can achieve this desired behaviour.
Basically, the idea is to modify your getItemViewType to pass the proper view in your onBindViewHolder. Your getItemViewType might look like this.
#Override
public int getItemViewType(int position) {
if (mListItemArray.get(position).curriculumName != null) {
// This is where we'll add header.
return HEADER_VIEW;
}
return super.getItemViewType(position);
}