Remove a Row from recycle view and Firebase - java

Hi I am trying to remove A row from a recycle view and once it is removed from the recycle view it should also be removed from the firebase realtime database. I am able to remove it from the recycle view but when I go back into my activity it repopulates with all the rows as I am unsure on how to remove it from Firebase. Can anyone help?
public class FindAllContacts extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
ArrayList<Contacts> contacts = new ArrayList<>();
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser firebaseUser = mAuth.getCurrentUser();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_all);
getSupportActionBar().setTitle("View All Contacts");
getAllContacts();
}
public void getAllContatcs() {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Contacts");
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for (DataSnapshot userSnapshot : snapshot.getChildren()) {
Contact contact = userSnapshot.getValue(Contact.class);
assert contact != null;
contatcs.add(contact);
recyclerView = findViewById(R.id.findAllRV);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(FindAll.this));
adapter = new MainAdapter(contacts);
new ItemTouchHelper(itemTouch).attachToRecyclerView(recyclerView);
recyclerView.setAdapter(adapter);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Toast.makeText(FindAll.this, "Error Occurred: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
ItemTouchHelper.SimpleCallback itemTouch = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
contatcs.remove(viewHolder.getAdapterPosition());
adapter.notifyDataSetChanged();
}
};
}

Related

How to get specific name inside child firebase database into recyclerview?

I kinda confused, how to get specific name inside child key, i only know set recyclerview, but don't know how to get grandprice set to TextView, because grandprice is inside another child, my database down below.
key is CardView and grandprice is TextView.
this is my code
//RecyclerView
mRecyclerView = view.findViewById(R.id.recommeneded);
mRecyclerView.setHasFixedSize(true);
//mRecyclerView.setLayoutManager(new LinearLayoutManager(HomeActivity.this));
final RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 1);
mRecyclerView.setLayoutManager(mLayoutManager);
mProgressBar = view.findViewById(R.id.myDataLoaderProgressBar);
mProgressBar.setVisibility(View.VISIBLE);
final int position = 0;
recommended = new ArrayList<>();
mAdapter = new InvoiceAdapter(getActivity(), recommended);
mRecyclerView.setAdapter(mAdapter);
//mAdapter.setOnItemClickListener(HomeActivity.this);
rootRef = FirebaseDatabase.getInstance().getReference();
usersRef = rootRef.child("order");
uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDBListener = usersRef.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
recommended.clear();
for (DataSnapshot invoiceSnapshot : dataSnapshot.getChildren()) {
InvoiceModels upload = invoiceSnapshot.getValue(InvoiceModels.class);
upload.setKey(invoiceSnapshot.getKey());
recommended.add(position, upload);
}
mAdapter.notifyDataSetChanged();
mProgressBar.setVisibility(View.GONE);
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
mProgressBar.setVisibility(View.INVISIBLE);
}
});
//RecyclerView
my adapter
#Override
public void onBindViewHolder(RecyclerViewHolder holder, final int position) {
InvoiceModels currentProduct = recomended.get(position);
//holder.totalPrice.setText(currentProduct.getTotalPrice());
Toast.makeText(mContext, currentProduct.getTotalPrice(), Toast.LENGTH_SHORT).show();
}
#Override
public int getItemCount() {
return recomended.size();
}
public class RecyclerViewHolder extends RecyclerView.ViewHolder {
TextView totalPrice;
public RelativeLayout relativelayout;
public RecyclerViewHolder(View itemView) {
super(itemView);
totalPrice = itemView.findViewById(R.id.totalPrice);
relativelayout = (RelativeLayout) itemView.findViewById(R.id.relativeLayout);
}
}
Thank for helping, sincerely,
AL.
First you need to get the id of that inv-details. Next you can call the data inside it. You can try this.
recommended.clear();
//At here you get the id first.
final String keyOfChild = dataSnapshot.getKey();
usersRef.child(uid).child(keyOfChild).child("inv-details").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
InvoiceModels upload = dataSnapshot.getValue(InvoiceModels.class);
upload.setKey(invoiceSnapshot.getKey());
recommended.add(position, upload);
mAdapter.notifyDataSetChanged();
mProgressBar.setVisibility(View.GONE);
}

error E/RecyclerView: No adapter attached; skipping layout when setting a firebase recyclerview android studio [duplicate]

This question already has answers here:
Firebase:Recycler view No Adater attached , Skipping Layout
(3 answers)
Closed 2 years ago.
i got an eror in my recyclerview code, this is just a simple code that retrieve a child from uid database but it doesn't show anything and i get this E/RecyclerView: No adapter attached; skipping layout error in my logcat
here's my MainActivity
public class ProsesPesananActivity extends AppCompatActivity {
DatabaseReference database;
RecyclerView recyclerView;
ArrayList<Status> list;
StatusAdapter adapter;
String uid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_proses_pesanan);
final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
uid = firebaseAuth.getUid();
recyclerView = (RecyclerView) findViewById(R.id.list_proses);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<Status>();
database = FirebaseDatabase.getInstance().getReference().child("PasFoto").child(uid);
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
{
Status s = dataSnapshot.getValue(Status.class);
list.add(s);
}
adapter = new StatusAdapter(ProsesPesananActivity.this,list);
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProsesPesananActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
}
here's my Adapter
public class StatusAdapter extends RecyclerView.Adapter<StatusAdapter.MyViewHolder> {
Context context;
ArrayList<Status> statuses;
public StatusAdapter (Context c, ArrayList<Status> s){
context = c;
statuses = s;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_status,parent,false));
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.shopId.setText(statuses.get(position).getShopId());
}
#Override
public int getItemCount() {
return statuses.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
TextView shopId, ukuran, jumlah, total, status;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
shopId = (TextView) itemView.findViewById(R.id.tv_shopId);
ukuran = (TextView) itemView.findViewById(R.id.tv_ukuran);
jumlah= (TextView) itemView.findViewById(R.id.tvDetailJumlah);
total = (TextView) itemView.findViewById(R.id.tv_total);
}
}
}
as you can see i tried to retrive the data from firebase into my recyclerview using card layout, but i got nothing instead
The error saying no adapter attached means just that. It probably caused by ValueEventListener.onDataChange() never get called.
Nevertheless, a better practice would be setting the adapter beforehand and notify the adapter of any changes made. Whatever your intention is, make sure that ValueEventListener.onDataChange() gets called at least once, or else it might not work the way you originally wanted. In that case, try to study the actual ValueEventListener usage.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* ... your code ... */
list = new ArrayList<Status>();
adapter = StatusAdapter(this, list);
// Edit: Set adapter to RecyclerView`
recyclerView = (RecyclerView) findViewById(R.id.list_proses);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
database = FirebaseDatabase.getInstance().getReference().child("PasFoto").child(uid);
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
{
Status s = dataSnapshot.getValue(Status.class);
list.add(s);
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProsesPesananActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
Additionally, Jetpack Paging library is highly recommended in dealing with RecyclerView as it can conserve data & memory usage although the implementation steps are a bit not-really-straight-forward, but sure is fun once you get the hang of it.

Remove RecyclerView Firebase Item Android Java

I've created a favourites page using a RecyclerView with data saved on firebase. I'm attempting to delete an item from both firebase and the recyclerview in realtime. I can remove the item using
dataSnapshot.child(countries.get(position).getId()).getRef().removeValue();
countries.remove(position);
However, I can't remove the last item, but it deletes in firebase. I've tried different combinations of
notifyDataSetChanged();
notifyItemRemoved(position);
notifyItemRangeChanged(position, countries.size());
But with no luck so far, I can't see where I'm going wrong.
Populating recyclerview
Public class FavouritesFragment extends Fragment {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
RecyclerView.Adapter adapter;
ArrayList<Country> countries;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_favourites, container, false);
getActivity().setTitle("Favourites");
firebaseAuth = FirebaseAuth.getInstance();
databaseReference = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
String userID = user.getUid();
databaseReference = databaseReference.child("users").child(userID).child("Favourites");
recyclerView = view.findViewById(R.id.favourites_recycler);
countries = new ArrayList<>();
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
addFavourites();
return view;
}
public void addFavourites(){
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
Country c = ds.getValue(Country.class);
countries.add(c);
}
adapter = new FavouritesAdapter(countries);
recyclerView.setAdapter(adapter);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Deleting from RecyclerView in adapter class
public class FavouritesAdapter extends RecyclerView.Adapter<FavouritesAdapter.MyViewHolder>{
ArrayList<Country> countries;
private View view;
ImageBindingAdapter imageBindingAdapter = new ImageBindingAdapter();
RecyclerView recyclerView;
private int position;
private FirebaseDatabase firebaseDatabase;
private FirebaseAuth firebaseAuth;
private DatabaseReference databaseReference;
public FavouritesAdapter(ArrayList<Country> countries1){
countries = countries1;
}
#NonNull
#Override
public FavouritesAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.favourites, viewGroup, false);
recyclerView = view.findViewById(R.id.favourites_recycler);
firebaseAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference();
FirebaseUser user = firebaseAuth.getCurrentUser();
String userID = user.getUid();
databaseReference = databaseReference.child("users").child(userID).child("Favourites");
return new FavouritesAdapter.MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final FavouritesAdapter.MyViewHolder myViewHolder, final int i) {
myViewHolder.name.setText(countries.get(i).getName());
myViewHolder.snippet.setText(countries.get(i).getSnippet());
myViewHolder.popUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(view.getContext(), view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_to_favs:
deleteFavourite();
break;
default:
return false;
}
return true;
}
});
}
});
#Override
public int getItemCount() {
return countries.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public ImageView popUp;
public MyViewHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.cityName);
popUp = itemView.findViewById(R.id.dotsPopUp);
}
}
public void deleteFavourite(){
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String id = countries.get(position).getId();
if(dataSnapshot.exists() && dataSnapshot.child(id).exists()){
dataSnapshot.child(countries.get(position).getId()).getRef().removeValue();
countries.remove(position);
// notifyDataSetChanged();
// notifyItemRemoved(position);
// notifyItemRangeChanged(position, countries.size());
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
}); }
}
In your adapter you didn't set the position to the position of the item you need to delete. So when the user hits the delete item in the popUp menu, you call deleteFavourite();, but you need to set the class field position before calling deleteFavourite().
So, in onBindViewHolder, you need to assign the value of the second parameter to the position, and it's better to pass it to deleteFavourite() method as a paraemter like deleteFavourite(position):
#Override
public void onBindViewHolder(#NonNull final FavouritesAdapter.MyViewHolder myViewHolder, final int pos) {
....
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_to_favs:
deleteFavourite(pos);
break;
default:
return false;
}
return true;
}
});
Then change your deleteFavourite to accept the new parameter, and notifyItemRemoved(position);
public void deleteFavourite(int pos){
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
String id = countries.get(pos).getId();
if(dataSnapshot.exists() && dataSnapshot.child(id).exists()){
dataSnapshot.child(countries.get(pos).getId()).getRef().removeValue();
countries.remove(pos);
notifyItemRemoved(pos);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
Do this to remove object from list, in deleteFavourite() function:
..........
if(dataSnapshot.exists() && dataSnapshot.child(id).exists()){
dataSnapshot.child(countries.get(position).getId()).getRef().removeValue();
countries.remove(countries.get(position));
notifyDataSetChanged();
}
..............

RecyclerView shows the same item many times in my app

I've created a match system in my Android program that also includes a chat section which has 2 fragments. The first fragment shows your chats, the second one shows your matches by using RecyclerView and UsersAdapter. However, in the second (match) fragment, people who you matched with are shown many times. For example, the same person is seen many times as you can see here.
My code is below.
UserFragment:
public class UsersFragment extends Fragment {
private RecyclerView recyclerView;
private UsersAdapter usersAdapter;
private List<User> mUsers;
FirebaseFirestore usersfragmentstore;
FirebaseAuth readauth;
DocumentReference readref;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
usersfragmentstore = FirebaseFirestore.getInstance();
readauth = FirebaseAuth.getInstance();
View view = inflater.inflate(R.layout.fragment_users, container, false);
readref = usersfragmentstore.collection("users").document(readauth.getCurrentUser().getUid());
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mUsers = new ArrayList<>();
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("users");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
readref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot shot = task.getResult();
List<String> matchedid2 = new ArrayList<>();
matchedid2 = (List<String>) shot.get("matchlist");
if (matchedid2.contains(snapshot.getKey())) {
User user = snapshot.getValue(User.class);
mUsers.add(user);
usersAdapter = new UsersAdapter(getContext(), mUsers, false);
recyclerView.setAdapter(usersAdapter);
}
}
});
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
return view;
}
}
UserAdapter:
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.ViewHolder> {
private Context mContext;
private List<User> mUsers;
private boolean ischat;
String theLastMessage;
public UsersAdapter(Context mContext, List<User> mUsers, boolean ischat) {
this.mUsers = mUsers;
this.mContext = mContext;
this.ischat = ischat;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.user_item_chat, parent, false);
return new UsersAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
User user = mUsers.get(position);
holder.username.setText(user.getName());
if (user.getImageURL().equals("default")) {
holder.profile_image.setImageResource(R.drawable.profilepicture);
} else {
Glide.with(mContext).load(user.getImageURL()).into(holder.profile_image);
}
if (ischat) {
lastMessage(user.getId(), holder.last_msg);
} else {
holder.last_msg.setVisibility(View.GONE);
}
if (ischat) {
if (user.getStatus().equals("online")) {
holder.img_on.setVisibility(View.VISIBLE);
holder.img_off.setVisibility(View.GONE);
} else {
holder.img_on.setVisibility(View.GONE);
holder.img_off.setVisibility(View.VISIBLE);
}
} else {
holder.img_on.setVisibility(View.GONE);
holder.img_off.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext, MessageActivity.class);
intent.putExtra("userid", user.getId());
mContext.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return mUsers.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView username;
public ImageView profile_image;
private ImageView img_on;
private ImageView img_off;
private TextView last_msg;
public ViewHolder(#NonNull View itemView) {
super(itemView);
username = itemView.findViewById(R.id.chatusername);
profile_image = itemView.findViewById(R.id.profile_image);
img_on = itemView.findViewById(R.id.img_on);
img_off = itemView.findViewById(R.id.img_off);
last_msg = itemView.findViewById(R.id.last_msg);
}
}
private void lastMessage(final String userid, final TextView last_msg) {
theLastMessage = "defalut";
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Chat chat = snapshot.getValue(Chat.class);
if (firebaseUser != null && chat != null) {
if (chat.getReceiver().equals(firebaseUser.getUid()) && chat.getSender().equals(userid) || chat.getReceiver().equals(userid) && chat.getSender().equals(firebaseUser.getUid())) {
theLastMessage = chat.getMessage();
}
}
}
switch (theLastMessage) {
case "default":
last_msg.setText("No message");
break;
default:
last_msg.setText(theLastMessage);
break;
}
theLastMessage = "default";
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
What am I missing?
befor adding new data first clear list by mUsers.clear(); as below
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
readref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
DocumentSnapshot shot = task.getResult();
List<String> matchedid2 = new ArrayList<>();
matchedid2 = (List<String>) shot.get("matchlist");
if (matchedid2.contains(snapshot.getKey())) {
mUsers.clear();
User user = snapshot.getValue(User.class);
mUsers.add(user);
usersAdapter = new UsersAdapter(getContext(), mUsers, false);
recyclerView.setAdapter(usersAdapter);
}
}
});
}

Adding values from Firebase to RecycleView adapter won't show in app

I'm trying to return the key and value from my Firebase Database and insert it into a RecycleView adapter but the values won't show up in the app.
I can't wrap my head around this whatsoever.
I tried to show the values in a Toast and it worked but why won't the values be inserted into the RecycleView adapter?
This is the code.
public class test extends AppCompatActivity {
TextView textView;
Button button;
String phone;
String n;
String a;
FirebaseAuth mAuth;
MyRecyclerViewAdapter adapter;
ArrayList<String> info;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Bundle b = new Bundle();
b = getIntent().getExtras();
phone = b.getString("number");
textView = findViewById(R.id.textV);
button = findViewById(R.id.signoutButton);
mAuth = FirebaseAuth.getInstance();
textView.setText(textView.getText().toString() + " as " + phone);
info = new ArrayList<>();
FirebaseDatabase myRef = FirebaseDatabase.getInstance();
DatabaseReference sRef = myRef.getReference("users");
sRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnap : dataSnapshot.getChildren()) {
if (childSnap.getKey().equals(phone)) {
for (DataSnapshot c : childSnap.getChildren()) {
n = c.getKey();
a = c.getValue().toString();
info.add(n);
info.add(a);
}
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
RecyclerView recyclerView = findViewById(R.id.rvInfo);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyRecyclerViewAdapter(this, info);
adapter.setClickListener(new MyRecyclerViewAdapter.ItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Toast.makeText(test.this, "You clicked " + adapter.getItem(position) + " on row number " + position, Toast.LENGTH_SHORT).show();
}
});
recyclerView.setAdapter(adapter);
}
}
You can get this using following code
sRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnap : dataSnapshot.getChildren()) {
if (childSnap.getKey().equals(phone)) {
for (DataSnapshot c : childSnap.getChildren()) {
n = c.getKey();
a = c.getValue().toString();
info.add(n);
info.add(a);
}
}
}
notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
You have to add notifyDataSetChanged(); after you fill ArrayList from firebase. Because it's take time to load data from firebase. And your adapter initialised before fill ArrayList from firebase.

Categories