Recycler View doesn't show any item - java

I am trying to display contacts in a Fragment but the recycler view doesn't show any item. Despite having items in the list recycler view is not showing any item. I am not sure what I have done wrong as the app does not crash when it is running.
Here is The Code
public class UsersFragment extends Fragment {
RecyclerView recyclerView;
UsersAdapter adapter;
List<Users> actualuserList;
List<String> contactsList;
DatabaseReference reference;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_users, container, false);
recyclerView = view.findViewById(R.id.userRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
contactsList = new ArrayList<>();
actualuserList = new ArrayList<>();
return view;
}
private void getContactList() {
Cursor cursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
while (cursor.moveToNext()) {
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (contactsList.contains(phone)) {
cursor.moveToNext();
} else {
contactsList.add(phone);
}
}
getActualUser();
Log.d("contact: ", String.valueOf(contactsList.size()));
}
private void getActualUser() {
reference = FirebaseDatabase.getInstance().getReference().child("Users");
for (String number : contactsList) {
Query query = reference.orderByChild("phoneNumber").equalTo(number);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
actualuserList.clear();
if (snapshot.exists()) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Users user = dataSnapshot.getValue(Users.class);
actualuserList.add(user);
}
Log.d("actual: ", String.valueOf(actualuserList.size()));
adapter = new UsersAdapter(getContext(), actualuserList);
recyclerView.setAdapter(adapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getContactList();
}
}
In the Log statement, it is showing actual:1 that means there is an item.
Here is Adapter Code
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.ViewHolder> {
Context mContext;
List<Users> mList;
public UsersAdapter(Context mContext, List<Users> mList) {
this.mContext = mContext;
this.mList = mList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.display_contacts, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
final Users users = mList.get(position);
holder.name.setText(users.getUsername());
holder.number.setText(users.getPhoneNumber());
String url = users.getProfilephotoURL();
if (url.equals(""))
holder.circleImageView.setImageResource(R.drawable.ic_user);
else
Glide.with(mContext).load(url).into(holder.circleImageView);
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(mContext, ChatActivity.class);
intent.putExtra("name", users.getUsername());
intent.putExtra("firendPhoneNumber", users.getPhoneNumber());
mContext.startActivity(intent);
});
}
#Override
public int getItemCount() {
return mList.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView name, number;
CircleImageView circleImageView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.contactName);
number = itemView.findViewById(R.id.contactNumber);
circleImageView = itemView.findViewById(R.id.profilePic);
}
}
}
Logs
2020-07-27 14:52:51.651 25366-25366/com.mycompany.newchatapp D/contact:: 167
2020-07-27 14:52:51.670 25366-25366/com.mycompany.newchatapp E/RecyclerView: No adapter attached; skipping layout
2020-07-27 14:52:51.691 25366-25366/com.mycompany.newchatapp W/ClassMapper: No setter/field for status found on class com.mycompany.newchatapp.Model.Users
2020-07-27 14:52:51.691 25366-25366/com.mycompany.newchatapp D/actual:: 1
2020-07-27 14:52:51.723 25366-25366/com.mycompany.newchatapp W/ClassMapper: No setter/field for status found on class com.mycompany.newchatapp.Model.Users
2020-07-27 14:52:51.723 25366-25366/com.mycompany.newchatapp D/actual:: 1
Any Idea why this is happening?

Seems like you forgot to set adapter.
In onCreateView method put something like:
recyclerView.setAdapter(new UsersAdapter(this.getActivity(), getContactList()));
UPD:
Before that, make getContactList() return relevant dataset. Do not set adapter after initializing recycler view.

Strangely, I've faced this issue very recently and I was using as global List variable as well and the list wasn't empty, even the adpater was not empty but RecyclerView wasn't showing any data until second adpater.notifyDataSetchanged().
I'd found an answer here on this site that said "create a new list variable" and it worked.
Try making the actualUserList a local variable as in your onDataChange(),
public void onDataChange(#NonNull DataSnapshot snapshot) {
List<Users> actualuserList = new ArrayList<>();
if (snapshot.exists()) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
Users user = dataSnapshot.getValue(Users.class);
actualuserList.add(user);
}
Log.d("actual: ", String.valueOf(actualuserList.size()));
adapter = new UsersAdapter(getContext(), actualuserList);
recyclerView.setAdapter(adapter);
}
}
If this doesn't solve your problem, you can also try calling notifyDataSetChanged() after setting up the adapter as adapter.notifyDataSetChanged();.
Also, set the adapter in onCreatedView() and call adapter.notifyDataSetChanged() in this onDataChange().

Related

Firebase Recycler View firstly showing blank page but when power off my mobile & open then showing data perfectly, no errors

I'm creating app with Firebase implementation. Now I'm stuck with showing list of data using recyclerView. After using my code I have empty activity without any error in Logcat but when turn off my mobile power button and again start then data show on recycleView. Also one thing when I go another activity and open this activity then again show blank page. I need someone's help :(
This is my fragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_message, container, false);
// Initialize
recyclerView = view.findViewById(R.id.recycleView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
list = new ArrayList<>();
// For retrieve Chat User ID
databaseReference = FirebaseDatabase.getInstance().getReference().child("Chat");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
list.clear();
for (DataSnapshot ds : snapshot.getChildren()) {
receiveID = ds.getKey();
// Restore Data on model class name UserDataInfo
databaseReference = FirebaseDatabase.getInstance().getReference().child("UsersData").child(receiveID);
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String userName = String.valueOf(snapshot.child("userName").getValue());
String userNumber = String.valueOf(snapshot.child("userPhone").getValue());
String userType = String.valueOf(snapshot.child("userType").getValue());
String userImage = String.valueOf(snapshot.child("userName").getValue());
info = new UserDataInfo(userImage, userType, userNumber, userName, receiveID);
list.add(info);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
// End
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
adapter = new messageListAdapter(list, getContext());
recyclerView.setAdapter(adapter);
}
My Adapter:
public class messageListAdapter extends RecyclerView.Adapter<messageListAdapter.ViewHolder> {
List<UserDataInfo> infoList;
Context context;
public messageListAdapter(List<UserDataInfo> infoList, Context context) {
this.infoList = infoList;
this.context = context;
}
#NonNull
#Override
public messageListAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list_show_admin, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull messageListAdapter.ViewHolder holder, int position) {
final UserDataInfo userDataInfo = infoList.get(position);
holder.Name.setText(userDataInfo.getUserName());
holder.Number.setText(userDataInfo.getUserPhone());
holder.whichType.setText(userDataInfo.getUserType());
holder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "ID: " + userDataInfo.getReceiveID(), Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return infoList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView Image;
TextView Name, Number, whichType;
LinearLayout linearLayout;
public ViewHolder(#NonNull View itemView) {
super(itemView);
Image = itemView.findViewById(R.id.UserImage);
Name = itemView.findViewById(R.id.Name);
Number = itemView.findViewById(R.id.Number);
whichType = itemView.findViewById(R.id.WhichType);
linearLayout = itemView.findViewById(R.id.Linear_layout);
}
}
}
my Firebase DataBase setup:
Database image
Database image

E/RecyclerView: No adapter attached; skipping layout D/OpenGLRenderer: eglDestroySurface = 0x730c9a02d0

I am facing these problem about Recyclerview No adapter attached.
This is my Locat:
E/RecyclerView: No adapter attached; skipping layout
D/OpenGLRenderer: eglDestroySurface = 0x730c9a02d0
D/ViewRootImpl#8aa7e83[AddPostActivity]: Relayout returned: old=[0,0]
[1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0}
changed=true
W/ClassMapper: No setter/field for YtyKXov13lReyirA8U4iritiUs6220-July-
201910: 39 found on class gurungdinesh.com.sisapen.ModelPost
W/ClassMapper: No setter/field for pdMZtSaxkzd5Uul592qwXKawnaI220-July-
201910: 40 found on class gurungdinesh.com.sisapen.ModelPost
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 4
I/zygote64: Do partial code cache collection, code=498KB, data=332KB
I/zygote64: After code cache collection, code=498KB, data=332KB
I/zygote64: Increasing code cache capacity to 2MB
D/ViewRootImpl#f8f3e40[MainActivity2]: MSG_RESIZED_REPORT: frame=Rect(0,
0 - 1080, 1920) ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
MSG_WINDOW_FOCUS_CHANGED 1
This is my Adapter:
public class AdapterPost extends
RecyclerView.Adapter<AdapterPost.MyHolder> {
Context context;
List<ModelPost> postList;
public AdapterPost(Context context, List<ModelPost>postList) {
this.context = context;
this.postList = postList;
}
#NonNull
#Override
public MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int
viewType) {
//inflate layout
View view =
LayoutInflater.from(context).inflate(R.layout.activity_users_posts,
viewGroup, false);
return new MyHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyHolder myHolder, int i) {
//get data
String fullname = postList.get(i).getFullname();
String date = postList.get(i).getDate();
String time = postList.get(i).getTime();
String Title = postList.get(i).getTitle();
String Writing = postList.get(i).getWriting();
//set data
myHolder.uNameTv.setText(fullname);
myHolder.pTimeTv.setText(time);
myHolder.pDate.setText(date);
myHolder.pTitileTv.setText(Title);
myHolder.pWritingTv.setText(Writing);
}
#Override
public int getItemCount() {
return postList.size();
}
//view holder class
class MyHolder extends RecyclerView.ViewHolder
{
//views from users post
ImageButton moreBtn;
CircularImageView uPictureIv;
TextView uNameTv, pTimeTv, pTitileTv, pWritingTv, pLikesTv, pDate;
Button likebtn, commentBtn, shareBtn;
private MyHolder(#NonNull View itemView) {
super(itemView);
//init views
uPictureIv = itemView.findViewById(R.id.uPictureTv);
uNameTv = itemView.findViewById(R.id.uNameTv);
pTimeTv = itemView.findViewById(R.id.pTimeTv);
moreBtn = itemView.findViewById(R.id.moreBtn);
pDate = itemView.findViewById(R.id.pDateTv);
pTitileTv = itemView.findViewById(R.id.pTitleTv);
pWritingTv = itemView.findViewById(R.id.pWriting);
pLikesTv = itemView.findViewById(R.id.pLikesTv);
likebtn = itemView.findViewById(R.id.likeBtn);
commentBtn = itemView.findViewById(R.id.commentBtn);
shareBtn = itemView.findViewById(R.id.shareBtn);
}
This is my HomeFragment:
public class HomeFragment extends Fragment {
private FirebaseAuth mFirebase;
private DatabaseReference databaseReference;
private RecyclerView recyclerView;
private List<ModelPost> postList;
private AdapterPost adapterPost;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.home_activity, container,
false);
//init
mFirebase = FirebaseAuth.getInstance();
databaseReference =
FirebaseDatabase.getInstance().getReference("posts");
recyclerView = view.findViewById(R.id.all_user_post_list);
LinearLayoutManager layoutManager = new
LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(layoutManager);
postList = new ArrayList<>();
loadPosts();
return view;
}
private void loadPosts() {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
postList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ModelPost modelPost = ds.getValue(ModelPost.class);
postList.add(modelPost);
//adapter
adapterPost = new AdapterPost(getActivity(), postList);
//set adapter to RecyclerView
recyclerView.setAdapter(adapterPost);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
//in case of error
Toast.makeText(getActivity(), "" +
databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
I have tried so many things but nothing happen.
Since Firebase Tasks are Async the setAdapter method is called after your return view; in onCreateView method. That's why it gives the error No adapter attached. What you can do is call setAdapter in the onCreateView method and after you have populated the list, call notifyDataSetChanged(). See the code below
public class HomeFragment extends Fragment {
private FirebaseAuth mFirebase;
private DatabaseReference databaseReference;
private RecyclerView recyclerView;
private List<ModelPost> postList;
private AdapterPost adapterPost;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.home_activity, container,
false);
//init
mFirebase = FirebaseAuth.getInstance();
databaseReference =
FirebaseDatabase.getInstance().getReference("posts");
recyclerView = view.findViewById(R.id.all_user_post_list);
LinearLayoutManager layoutManager = new
LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(layoutManager);
postList = new ArrayList<>();
// ----------- set the adapter here --------------------
adapterPost = new AdapterPost(getActivity(), postList);
recyclerView.setAdapter(adapterPost);
loadPosts();
return view;
}
private void loadPosts() {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
postList.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
ModelPost modelPost = ds.getValue(ModelPost.class);
postList.add(modelPost);
}
// ----------- call notifyDataSetChanged --------------------
adapterPost.notifyDataSetChanged()
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
//in case of error
Toast.makeText(getActivity(), "" +
databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
You get this message because RecyclerView has no adapter set when the layout is drawn.
What you can do is that you can assign an empty adapter at start, i.e. in onCreateView(), then add the data to your adapter when you have it. This way you will not see this log. However, the first place you set the adapter is in a asynchronous code block. Everything is already drawn and the recyclerview hasn't provided an adapter yet, so it prints this log.

FirebaseRecyclerAdapter (sending list to UserAdapter class)

I am new to Android Studio..so please help me out.
I am working on app which shows the list of student in recycler view,I am retriving the list from Fire base Database but i'm getting the error at UserAdapter constructor class (I don't know what to provide in Super()).
I have used standard RecyclerAdapter it work the way I wanted but I couldn't get the UID of student when person clicks on it ....so I switched to FirebaseRecyclerAdapter ...here I can retrive the UID easily ...but getting error at constructor of UserAdapter.
I think I have to pass the FirebaseRecyclerOptions in my case its "options" but its show error
"Cannot reference 'UserAdapter.options' before supertype constructor has been called" and I don't know how to make the options Static.
I tried to make it static like this
public static FirebaseRecyclerOptions options;
in classmates class and accessed it in super() as super(classmates.this.options);i know its wrong method and then I declared in UserAdapter class but didnt work.
sorry for the long post.please help me out.
Here's the code:
public class classmates extends AppCompatActivity {
Toolbar toolbar;
private RecyclerView ClassmateList;
private DatabaseReference rDatabaseRef;
List<Users> list = new ArrayList<>();
FirebaseRecyclerAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_classmates);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Classmates");
rDatabaseRef = FirebaseDatabase.getInstance().getReference().child("user");
ClassmateList = (RecyclerView) findViewById(R.id.recyclerView);
ClassmateList.setHasFixedSize(true);
ClassmateList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
rDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot Snapshot) {
for (DataSnapshot dataSnapshot : Snapshot.getChildren()) {
Users studentDetails = dataSnapshot.getValue(Users.class);
list.add(studentDetails);
}
adapter = new UserAdapter(list, classmates.this);
ClassmateList.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
public class UserAdapter extends FirebaseRecyclerAdapter<Users, UserAdapter.UserViewHolder> {
Context context1;
List<Users> InfoList;
FirebaseRecyclerOptions<Users> options =
new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(rDatabaseRef, Users.class)
.build();
public UserAdapter(List<Users> list, classmates classmates) {
super(); *<---ERROR ,DONT KNOW WHAT TO PROVIDE*
context1=classmates;
InfoList=list;
}
#Override
protected void onBindViewHolder(#NonNull UserViewHolder holder, int position, #NonNull Users model) {
final Users users = InfoList.get(position);
holder.setName(users.getName());
holder.setStatus(users.getStatus());
holder.setThumb_img(users.getThumb_img());
String userid=getRef(position).getKey();
}
#NonNull
#Override
public UserViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_singlelayout, parent, false);
UserViewHolder holder = new UserViewHolder(view);
return holder;
}
#Override
public int getItemCount() {
return InfoList.size();
}
public class UserViewHolder extends RecyclerView.ViewHolder {
View rview;
public UserViewHolder(View itemView) {
super(itemView);
rview = itemView;
}
public void setName(String name) {
TextView singleusername = (TextView) rview.findViewById(R.id.single_displayname);
singleusername.setText(name);
}
public void setStatus(String status) {
TextView singleuserstatus = (TextView) rview.findViewById(R.id.single_status);
singleuserstatus.setText(status);
}
public void setThumb_img(String thumb_image) {
CircleImageView user_img = (CircleImageView) rview.findViewById(R.id.single_thumb_image);
Picasso.get().load(thumb_image).placeholder(R.drawable.default_img).into(user_img);
}
}
}
}

How can I populate a spinner with my Firebase database?

I want to populate a spinner instead of a ListView with data from Firebase.
The code below works fine with ListView. But how can I replace the ListView with a spinner? Hope someone can help me with this.
My research:
Firebase data to Spinner
Populatate the spinner from Firebase database
Here is my Main Activity:
public class Ansattliste extends AppCompatActivity {
DatabaseReference databaseAnsatt;
ListView lvansattliste;
Button btnleggtilansatt;
List<Ansatt> listansatt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ansattliste);
databaseAnsatt = FirebaseDatabase.getInstance().getReference("Ansatte");
lvansattliste = (ListView)findViewById(R.id.lvansattliste);
listansatt = new ArrayList<>();
btnleggtilansatt = (Button)findViewById(R.id.btnleggtilansatt);
btnleggtilansatt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent leggtil = new Intent(Ansattliste.this, Leggtilansatt.class);
startActivity(leggtil);
}
});
}
#Override
protected void onStart() {
super.onStart();
databaseAnsatt.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
listansatt.clear();
for (DataSnapshot ansattSnapshot : dataSnapshot.getChildren()) {
final Ansatt ansatt = ansattSnapshot.getValue(Ansatt.class);
listansatt.add(ansatt);
final listAnsatt adapter = new listAnsatt(Ansattliste.this, listansatt);
lvansattliste.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled (DatabaseError databaseError){
}
});
}
}
And my Adapter:
public class listAnsatt extends ArrayAdapter<Ansatt> {
private Activity context;
private List<Ansatt> listansatt;
public listAnsatt(Activity context, List<Ansatt> listansatt) {
super(context, R.layout.list_ansatt, listansatt);
this.context = context;
this.listansatt = listansatt;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.list_ansatt, null, true);
TextView tvansattnavn = (TextView) listViewItem.findViewById(R.id.tvansattnavn);
TextView tvansaattnr = (TextView) listViewItem.findViewById(R.id.tvansattnr);
Ansatt ansatt = listansatt.get(position);
tvansattnavn.setText(ansatt.getAnsattnavn());
tvansaattnr.setText(ansatt.getAnsattnr());
return listViewItem;
}
Firebase:
Firebase
You'd do something like following (I've adapted from Kotlin code I have here so possibility of some syntax errors)...have assumed for illustration that getAnsattnavn() is value you want to show in spinner. You'd still have listansatt which presumably you'd look up when item is selected in spinner.
In onDataChange:
List<String> optionList = new ArrayList<>();
for (DataSnapshot ansattSnapshot : dataSnapshot.getChildren()) {
final Ansatt ansatt = ansattSnapshot.getValue(Ansatt.class);
optionList.add(ansatt.getAnsattnavn());
listansatt.add(ansatt);
}
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, optionList);
spinnerControl.setAdapter(adapter);

Display Firebase data into a custom adapter view within Fragment tab

This is the edited version of my Fragment class inside my main activity. There were some bugs- it was not displaying anything and it was no longer saving the data to the database.
public static class DummyFragment extends Fragment {
int color;
public DummyFragment() {
}
#SuppressLint("ValidFragment")
public DummyFragment(int color) {
this.color = color;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dummy_fragment, container, false);
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dummyfrag_scrollableview);
final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.dummyfrag_bg);
frameLayout.setBackgroundColor(color);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
DatabaseReference mDatabaseGig;
final List<Dessert> dessertList;
// get the gig database
mDatabaseGig = FirebaseDatabase.getInstance().getReference("Gig Posts");
dessertList = new ArrayList<>();
mDatabaseGig.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// dessertList.clear();
for(DataSnapshot gigSnapshot: dataSnapshot.getChildren()){
Dessert dessert = gigSnapshot.getValue(Dessert.class);
dessertList.add(dessert);
}
DessertAdapter adapter = new DessertAdapter(getContext());
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
// possible to put progress dialogue
return view;
}
}
I already got the saving to firebase working perfectly and I'm using the MVC model - I created an adapter, a model and wired it to the main activity but my problem is how to use this model into the onAddValueEventChangedListener();
public class DessertAdapter extendsRecyclerView.Adapter<DessertAdapter.DessertVh> {
private List<Dessert> desserts = new ArrayList<>();
private static final int VIEW_TYPE_EMPTY_LIST_PLACEHOLDER = 0;
private static final int VIEW_TYPE_OBJECT_VIEW = 1;
private Context context;
// TODO: placeholder stuff here
#Override
public int getItemViewType(int position) {
if (desserts.isEmpty()) {
return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
} else {
return VIEW_TYPE_OBJECT_VIEW;
}
}
public DessertAdapter(Context context) {
this.context = context;
this.desserts = desserts;
desserts = Dessert.prepareDesserts(
context.getResources().getStringArray(R.array.dessert_names),
context.getResources().getStringArray(R.array.dessert_descriptions),
context.getResources().getStringArray(R.array.dessert_amounts));
}
// TODO: another placeholder stuff here
#Override
public DessertVh onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_dessert, parent, false);
return new DessertAdapter.DessertVh(view);
}
#Override
public void onBindViewHolder(DessertVh holder, int position) {
Dessert dessert = desserts.get(position);
holder.mName.setText(dessert.getName());
holder.mDescription.setText(dessert.getDescription());
holder.mFirstLetter.setText(String.valueOf(dessert.getFirstLetter()));
holder.mPrice.setText(String.valueOf(dessert.getAmount()));
}
#Override
public int getItemCount() {
// if nothing, return null,
// else return the number of items in the list
return desserts == null ? 0 : desserts.size();
}
public static class DessertVh extends RecyclerView.ViewHolder {
private TextView mName;
private TextView mPrice;
private TextView mDescription;
private TextView mFirstLetter;
public DessertVh(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.txt_name);
mPrice = (TextView) itemView.findViewById(R.id.txt_price);
mDescription = (TextView) itemView.findViewById(R.id.txt_desc);
mFirstLetter = (TextView) itemView.findViewById(R.id.txt_firstletter);
}
}
}
main activity where the tabs are displayed
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs_header);
// get the gig database
mDatabaseGig = FirebaseDatabase.getInstance().getReference("Gig Posts");
dessertList = new ArrayList<>();
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this , this )
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
This handles the display of the data
#Override
public void onStart(){
super.onStart();
mFirebaseAuth.addAuthStateListener(firebaseAuthListener);
// load the data from database here
mDatabaseGig.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
dessertList.clear();
for(DataSnapshot gigSnapshot: dataSnapshot.getChildren()){
Dessert dessert = gigSnapshot.getValue(Dessert.class);
dessertList.add(dessert);
}
// maybe this will work?
DummyFragment dummyFragment = new DummyFragment();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
some code missing here but still part of mainactivity
This is the Dummy Fragment Class inside the main activity
public static class DummyFragment extends Fragment {
int color;
public DummyFragment() {
}
#SuppressLint("ValidFragment")
public DummyFragment(int color) {
this.color = color;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dummy_fragment, container, false);
final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.dummyfrag_bg);
frameLayout.setBackgroundColor(color);
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dummyfrag_scrollableview);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
// possible to put progress dialogue
DessertAdapter adapter = new DessertAdapter(getContext());
recyclerView.setAdapter(adapter);
return view;
}
}
Put the Firebase code inside the Fragment, not the Activity and also pass the dessertList as a param to the DessertAdapter.
Example:
public static class DummyFragment extends Fragment {
int color;
public DummyFragment() {
}
#SuppressLint("ValidFragment")
public DummyFragment(int color) {
this.color = color;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dummy_fragment, container, false);
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dummyfrag_scrollableview);
final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.dummyfrag_bg);
frameLayout.setBackgroundColor(color);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
DatabaseReference mDatabaseGig;
final List<Dessert> dessertList;
// get the gig database
mDatabaseGig = FirebaseDatabase.getInstance().getReference("Gig Posts");
dessertList = new ArrayList<>();
DessertAdapter adapter = new DessertAdapter(getContext(), dessertList);
recyclerView.setAdapter(adapter);
mDatabaseGig.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// dessertList.clear();
for(DataSnapshot gigSnapshot: dataSnapshot.getChildren()){
Dessert dessert = gigSnapshot.getValue(Dessert.class);
dessertList.add(dessert);
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
// possible to put progress dialogue
return view;
}
}

Categories