I'm new to Android and I know this might be a piece of cake but I'm struggling.
I was trying to create a dynamic expandable recycler view using firebase realtime database but the data is not displaying properly.
I'm passing a string from one activity to another and comparing the string to the database field and then displaying it's child components but I don't know where I'm failing.
I want to display the Parent and child field by comparing the name field with the value.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_detail);
//string value from another activity
String value = getIntent().getExtras().getString("name");
recycler_view = (RecyclerView) findViewById(R.id.recycler_expand);
recycler_view.setLayoutManager(new LinearLayoutManager(this));
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference parentReference = database.getReference().child("Tutors");
//comparing
parentReference.equalTo(value).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<ParentList> Parent = new ArrayList<>();
for (final DataSnapshot snapshot : dataSnapshot.getChildren()){
final String ParentKey = snapshot.getKey().toString();
snapshot.child("title").getValue();
DatabaseReference childReference =
FirebaseDatabase.getInstance().getReference().child(ParentKey);
childReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<ChildList> Child = new ArrayList<>();
for (DataSnapshot snapshot1:dataSnapshot.getChildren())
{
final String ChildValue = snapshot1.getValue().toString();
snapshot1.child("title").getValue();
Child.add(new ChildList(ChildValue));
}
Parent.add(new ParentList(ParentKey, Child));
DocExpandableRecyclerAdapter adapter = new DocExpandableRecyclerAdapter(Parent);
recycler_view.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
System.out.println("Failed to read value." + error.toException());
}
});}}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Database Structure
ChildList.java
public class ChildList implements Parcelable {
private String title;
public ChildList(String title) {
this.title = title;
}
protected ChildList(Parcel in) {
title = in.readString();
}
public static final Creator<ChildList> CREATOR = new Creator<ChildList>() {
#Override
public ChildList createFromParcel(Parcel in) {
return new ChildList(in);
}
#Override
public ChildList[] newArray(int size) {
return new ChildList[size];
}
};
public String getTitle() {
return title;
}
public void setTitle(String Title) {
this.title = Title;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(title);
}
}
Please anyone help me with this :(
Don't use equalTo().
Try this:
DatabaseReference parentReference = database.getReference().child("Tutors");
parentReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (final DataSnapshot snapshot : dataSnapshot.getChildren()){
final String NameKey = snapshot.getKey().toString();
if(value.equals(NameKey)){
//DO WHATEVER YOU WANT
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Related
In the below code, At start I am retrieving user's profile info which they saved in previous activity(This is working fine)
Now, user tries to save some targets in firebase which I want to retrieve in recyclerView whenever it is saved.There is no error in saving the data.
My app crashes whenever I tries to retrieve data into recyclerView. It works fine if I retrieve a particular data into a textView.
I have tried various ways given on Stack OverFlow but nothing seems to be working
Please help me with this.
ProfileActivity.class
#Override
protected void onStart() {
super.onStart();
//retrieving profile data
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
reference = FirebaseDatabase.getInstance().getReference("User's Details").child(uid).child("profile info");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
String name= (String) snapshot.child("name").getValue();
String bio = (String) snapshot.child("bio").getValue();
String imageUrl = (String) snapshot.child("imageUrl").getValue();
final_name.setText(name);
final_Bio.setText(bio);
picasso.get().load(imageUrl).into(final_profileImage);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
Log.w("TAG",error.getMessage());
Toast.makeText(Doc_ProfilePage.this,error.getMessage(),Toast.LENGTH_SHORT).show();
}
});
//saving challenge
private void SaveChallenge(String titleText, String description) {
int year = myCalender.get(Calendar.YEAR);
int month = myCalender.get(Calendar.MONTH);
int day = myCalender.get(Calendar.DAY_OF_MONTH);
String dateText = new StringBuilder().append(day).append("/").append(month).append("/").append(year).toString();
//getting TimeFormat
int hour = myCalender.get(Calendar.HOUR);
int minute = myCalender.get(Calendar.MINUTE);
String timeText = new StringBuilder().append(hour).append(":").append(minute).toString();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
DatabaseReference Challengereference = FirebaseDatabase.getInstance().getReference("User's Details").child(uid).child("ChallengeDetails");
if (!timeText.equals("") && !titleText.isEmpty() && !description.isEmpty()) {
id = Challengereference.push().getKey();
ChallengeDetails ChallengeDetails = new ChallengeDetails(titleText, description, dateText, timeText);
Challengereference.child(id).setValue(ChallengeDetails).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(Doc_ProfilePage.this, "Successfully saved", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(Doc_ProfilePage.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
//adding data in recyclerView
FirebaseDatabase.getInstance().getReference("User's Details").child(uid).child("ChallengeDetails").orderByKey().
addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
final ChallengeDetails challengeDetails = snapshot.getValue(ChallengeDetails.class);
challengedetail.add(challengeDetails);
GridLayoutManager layoutManager = new GridLayoutManager(Doc_ProfilePage.this,3,RecyclerView.HORIZONTAL,false);
recyclerView.setLayoutManager(layoutManager);
GridAdapter adapter = new GridAdapter(challengedetail);
recyclerView.setAdapter(adapter);
}
#Override
public void onChildChanged(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot snapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
GridAdapter class
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
private static final String TAG = "ActivityName";
private List<ChallengeDetails> Challengedetails;
public GridAdapter( List<ChallengeDetails> detailss) {
// this.mcontext = context;
this.Challengedetails = detailss;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.challenge_grid_view,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final ChallengeDetails challenge = Challengedetails.get(position);
holder.ChallengeTitleGrid.setText(challenge.getTitle());
holder.ChallengeDescriptionGrid.setText(challenge.getDescription());
holder.ChallengeGridDate.setText(challenge.getDate());
holder.ChallengeGridTime.setText(challenge.getTime());
}
#Override
public int getItemCount() {
return Challengedetails.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView ChallengeTitleGrid, ChallengeDescriptionGrid, ChallengeGridDate, ChallengeGridTime;
public ViewHolder(#NonNull View itemView) {
super(itemView);
ChallengeTitleGrid = itemView.findViewById(R.id.ChallengeTitleGrid);
ChallengeDescriptionGrid = itemView.findViewById(R.id.ChallengeDescriptionGrid);
ChallengeGridDate = itemView.findViewById(R.id.SelectDate);
ChallengeGridTime = itemView.findViewById(R.id.SelectTime);
}
}}
ChallengeDetails Class
public class ChallengeDetails {
String title;
String description;
String date;
String time;
public void setTitle(String title) {
this.title = title;
}
public void setDescription(String description) {
this.description = description;
}
public void setDate(String date) {
this.date = date;
}
public void setTime(String time) {
this.time = time;
}
public ChallengeDetails(String title, String description, String date, String time){
this.title = title;
this.description = description;
this.date = date;
this.time = time;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public String getDate() {
return date;
}
public String getTime() {
return time;
}
}
Image of Firebase Database
I think orderbychild send you array of datasnapshot not single document snapshot so you have to iterate through that snapshot.
public void onChildAdded(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
final ChallengeDetails challengeDetails = snapshot.getValue(ChallengeDetails.class);
challengedetail.add(challengeDetails);
}
GridLayoutManager layoutManager = new GridLayoutManager(Doc_ProfilePage.this,3,RecyclerView.HORIZONTAL,false);
recyclerView.setLayoutManager(layoutManager);
GridAdapter adapter = new GridAdapter(challengedetail);
recyclerView.setAdapter(adapter);
}
I'm working on this project where users are able to upload more than one character image along with other information. Such as the name of the character, what the character will do, the color of the character and its height. Now everything saves successfully in the database. However, when I want to display all of the character's information on the user's profile page, using a recyclerView, the saved information in the database will not display on the user's profile page. Below is my code. Thanks in advance
//Users profile page
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
overridePendingTransition(R.anim.slide_right, R.anim.slide_left);
mUploads = new ArrayList<>();
mAdapter = new ImageAdapter(getApplicationContext(), mUploads);
recyclerView = findViewById(R.id.recyclerView);
//recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
if (firebaseAuth != null) {
databaseReference = FirebaseDatabase.getInstance().getReference("Users");
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference();
//upload = findViewById(R.id.button_upload);
//button2 = findViewById(R.id.submit);
//imageView = findViewById(R.id.view_image);
//upload.setOnClickListener(this);
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
String imageUrl = (String) postsnapshot.child("mImageUrl").getValue();
mUploads.add(imageUrl);
//mUploads = new ArrayList<>();
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
//Model Upload.class
public class Upload {
private String mImageUrl;
public Upload(){
//Empty constructor needed
}
public Upload (String imageUrl){
mImageUrl=imageUrl;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
}
// Image adapter class
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder>{
private Context mContext;
private List<String> mUploads;
public ImageAdapter(Context context, List<String> uploads){
mContext = context;
mUploads = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View V = LayoutInflater.from(mContext).inflate(R.layout.cardview, parent, false);
return new ImageViewHolder(V);
}
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
String uploadCurrent=mUploads.get(position);
Picasso.get().load(uploadCurrent).fit().centerCrop().into(holder.imageView);
}
#Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public ImageView imageView;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.imageview);
}
}
}
Change this:
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
String imageUrl = (String) postsnapshot.child("mImageUrl").getValue();
mUploads.add(imageUrl);
//mUploads = new ArrayList<>();
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
into this:
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
String imageUrl = postsnapshot.child("mImageUrl").getValue(String.class);
mUploads.add(imageUrl);
//mUploads = new ArrayList<>();
mAdapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
If you want to read inside the children of children, you need to do double loop like this.
databaseReference.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
for (DataSnapshot snapshot : postsnapshot.getChildren()) {
String imageUrl = snapshot.child("mImageUrl").getValue(String.class);
mUploads.add(imageUrl);
//mUploads = new ArrayList<>();
}
}
mAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ProfileActivity.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
Edited:
If you want to pass all of them, you need to change a bit of your model class
public class Upload {
private String mImageUrl;
private String Character_Name;
private String What_Character_Does;
private String color;
private String height;
public Upload() {
//Empty constructor needed
}
public Upload(String imageUrl) {
mImageUrl = imageUrl;
}
public String getmImageUrl() {
return mImageUrl;
}
public void setmImageUrl(String mImageUrl) {
this.mImageUrl = mImageUrl;
}
public String getCharacter_Name() {
return Character_Name;
}
public void setCharacter_Name(String character_Name) {
Character_Name = character_Name;
}
public String getWhat_Character_Does() {
return What_Character_Does;
}
public void setWhat_Character_Does(String what_Character_Does) {
What_Character_Does = what_Character_Does;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
}
This question already has answers here:
How to update RecyclerView Adapter Data
(16 answers)
Closed 3 years ago.
I develop a chat app. I try to get data from firebase database and display it in a recyclerview, but it doesn't work. I searched it on Google and I tried a lot of options, but it displays nothing. What am I doing wrong?
These are the activities:
1.Main Activity(Where I want to display the data)
public class SearchActivity extends AppCompatActivity {
Toolbar sToolbar;
SearchView searchView;
RecyclerView recyclerView;
BottomNavigationView sBnv;
SearchAdapter searchAdapter;
LinearLayoutManager llm;
ArrayList<String> arrayList;
User user;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
sToolbar = findViewById(R.id.sToolbar);
recyclerView = findViewById(R.id.sRecyclerView);
sBnv = findViewById(R.id.sBnv);
searchAdapter = new SearchAdapter(arrayList, getApplicationContext());
recyclerView.setHasFixedSize(true);
llm = new LinearLayoutManager(getApplicationContext());
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String username = (String)
snapshot.child("username").getValue();
arrayList.add(username);
}
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(searchAdapter);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SearchActivity.this, "Database error", Toast.LENGTH_SHORT)
.show();
}
});
2.Adapter class:
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
ArrayList<String> friendsSearchedList = new ArrayList<>();
Context mContext;
public SearchAdapter(ArrayList<String> friendsSearchedList, Context mContext){
this.friendsSearchedList = friendsSearchedList;
this.mContext = mContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_user_item,parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final String username = friendsSearchedList.get(position);
holder.text.setText(username);
}
#Override
public int getItemCount() {
return friendsSearchedList == null ? 0:friendsSearchedList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView text, text1, text2;
Button button;
RelativeLayout relativeLayout;
public ViewHolder(View itemView){
super(itemView);
text = itemView.findViewById(R.id.sText);
button = itemView.findViewById(R.id.addFriend);
text1 = itemView.findViewById(R.id.sText1);
text2 = itemView.findViewById(R.id.sText2);
relativeLayout = itemView.findViewById(R.id.relativeLayout);
}
}
}
3.User class:
public class User {
public String email, id, password;
public String username;
public User(String email, String username, String password){
this.email = email;
this.username = username;
this.password = password;
}
public User(){}
public void setUsername(String username){
this.username = username;
}
public String getUsername(){
return username;
}
public void setEmail(String email){
this.email = email;
}
public String getEmail(){
return email;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
}
Initially the arraylist which your setting for the recyclerview is empty. When you are adding the data to that list you have to notify the recycler view adapter. So your code should look like this way:
searchAdapter = new SearchAdapter(arrayList, getApplicationContext());
// remove this line
/* recyclerView.setHasFixedSize(true);*/
llm = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(searchAdapter);
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users");
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String username = (String)
snapshot.child("username").getValue();
arrayList.add(username);
}
***searchAdapter.notifyDataSetChanged();***
}
}
If it’s a simple chat maybe a listview would be more convenient for you.
databaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
list.add(value);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
This should easily do the trick…
Create a method in your SearchAdapter.class like below
public void addData(ArrayList<String> list){
this.mDataSet = list;
notifyDataSetChanged();
}
And once you receive data from Firebase database, update it to the adapter like below.You need to tell the recycler view that your data set has been updated.
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String username = (String)
snapshot.child("username").getValue();
arrayList.add(username);
}
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(searchAdapter);
searchAdapter.addData(arrayList); // Add this line
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SearchActivity.this, "Database error", Toast.LENGTH_SHORT)
.show();
}
});
This is the screenshot of my database:
This is the activity:
private ListView dishList;
private Button bAddNewDish;
private DishListAdapter dishListAdapter;
private FirebaseDatabase mFireDb;
private DatabaseReference mDbReference;
private ChildEventListener mChildListener;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dishes);
dishList = (ListView) findViewById(R.id.dishList);
bAddNewDish = (Button) findViewById(R.id.bAddNewDish);
mFireDb = FirebaseDatabase.getInstance();
mDbReference = mFireDb.getReference().child("dishlist");
ArrayList<DishElement> dishArray = new ArrayList<>();
dishListAdapter = new DishListAdapter(this,R.layout.dish_item_element,dishArray);
dishList.setAdapter(dishListAdapter);
mChildListener = new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
DishElement dishElement = dataSnapshot.getValue(DishElement.class);
dishListAdapter.add(dishElement);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
DishElement dishElement = dataSnapshot.getValue(DishElement.class);
dishListAdapter.add(dishElement);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
DishElement dishElement = dataSnapshot.getValue(DishElement.class);
dishListAdapter.add(dishElement);
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
mDbReference.addChildEventListener(mChildListener);
bAddNewDish.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(),AddDish.class);
startActivity(i);
}
});
}
}
This is the adapter:
public class DishListAdapter extends ArrayAdapter<DishElement> {
public DishListAdapter(#NonNull Context context, int resource, ArrayList<DishElement> dishElements) {
super(context, resource, dishElements);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
DishElement dishElement = getItem(position);
if(convertView == null){
convertView = LayoutInflater.from((Activity) getContext()).inflate(R.layout.dish_item_element,parent,false);
}
TextView dishName = (TextView) convertView.findViewById(R.id.dishName);
dishName.setText(dishElement.getName());
return convertView;
}
}
I get null as value with dishElement.getName() method. The database connection seems to be working fine but I am not getting this string value whenever a child node is added in my database.
You have problems in reading data because your fields are not named correctly. To solve this, please use a model class that looks like this:
public class DishElement {
private String description, image, menuId, name;
private double price;
public DishElement() {}
public DishElement(String description, String image, String menuId, String name, double price) {
this.description = description;
this.image = image;
this.menuId = menuId;
this.name = name;
this.price = price;
}
public String getDescription() {return description;}
public String getImage() {return image;}
public String getMenuId() {return menuId;}
public String getName() {return name;}
public double getPrice() {return price;}
}
To actually read the data correctly, remove the old data from your database and add fresh one.
To read data, please use the following code:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference dishListRef = rootRef.child("dishlist");
ValueEventListener eventListener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
DishElement dishElement = ds.getValue(DishElement.class);
dishListAdapter.add(dishElement);
Log.d("TAG", dishElement.getName());
}
}
#Override
public void onCancelled(DatabaseError databaseError) {}
};
dishListRef.addListenerForSingleValueEvent(eventListener);
As you can see, I have added a log statement to print out the names using:
dishElement.getName()
In this case will work for sure. In your case didn't work because Firebase was looking for a field named name and not Name (capital N).
I'm trying to update and delete data in Firebase database.
SectionDetails model
public class SectionDetails {
private String sectionCode;
private String sectionSeats;
private String sectionKey;
public SectionDetails() {
}
public SectionDetails(String sectionCode, String sectionSeats) {
this.sectionCode = sectionCode;
this.sectionSeats = sectionSeats;
}
#Exclude
public String getSectionKey() {
return sectionKey;
}
public String getSectionCode() {
return sectionCode;
}
public String getSectionSeats() {
return sectionSeats;
}
}
FirebaseHelper class
public class FirebaseHelper {
DatabaseReference db;
ArrayList<SectionDetails> sectionDetailsArrayList = new ArrayList<>();
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
private void fetchData(DataSnapshot dataSnapshot) {
SectionDetails sectionDetails = dataSnapshot.getValue(SectionDetails.class);
sectionDetailsArrayList.add(sectionDetails);
adapter.notifyDataSetChanged();
}
public ArrayList<SectionDetails> retrieve() {
db.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
adapter.notifyDataSetChanged();
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
return sectionDetailsArrayList;
}
}
CustomAdapter class
public class CustomAdapter extends BaseAdapter {
DatabaseReference updateRef;
String key;
Context c;
ArrayList<SectionDetails> sectionDetailsArrayList;
public CustomAdapter(Context c, ArrayList<SectionDetails> sectionDetailsArrayList) {
this.c = c;
this.sectionDetailsArrayList = sectionDetailsArrayList;
}
#Override
public int getCount() {
return sectionDetailsArrayList.size();
}
#Override
public Object getItem(int position) {
return sectionDetailsArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final SectionDetails sd = (SectionDetails) this.getItem(position);
updateRef = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_COURSES).child("sections");
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog d = new Dialog(CustomAdapter.this);
d.setContentView(R.layout.section_custom_dialog);
Button btnUpdate = (Button) d.findViewById(R.id.btnUpdate);
Button btnDelete = (Button) d.findViewById(R.id.btnDelete);
btnUpdate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String code = "1B";
final String seats = "20";
updateRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
SectionDetails updateSD = snapshot.getValue(SectionDetails.class);
if (sd.getSectionCode().equals(updateSD.getSectionCode())) {
key = snapshot.getKey().toString();
}
}
SectionDetails newSD = new SectionDetails(code, seats);
updateRef.child(key).setValue(newSD);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
updateRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
SectionDetails deleteSD = snapshot.getValue(SectionDetails.class);
if (sd.getSectionCode().equals(deleteSD.getSectionCode())) {
updateSectionRef.child(snapshot.getKey().toString()).removeValue();
break;
}
}
adapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
d.show();
}
});
return convertView;
}
}
MainActivity class
public class MainActivity extends AppCompatActivity {
DatabaseReference mRef;
FirebaseHelper helper;
CustomAdapter adapter;
ListView lvSectionsListOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvSectionsListOne = (ListView) findViewById(R.id.lvSectionsList);
mRef = FirebaseDatabase.getInstance().getReference().child(Constants.FIREBASE_COURSES).child("sections");
helper = new FirebaseHelper(mRef);
adapter = new CustomAdapter(this, helper.retrieve());
lvSectionsListOne.setAdapter(adapter);
}
}
The data is deleted from database as expected, but the data that gets deleted remains inside the listview. I added adapter.notifyDataSetChanged() but still the listview is not updating.
The data is also updated as expected, but when update button is clicked, the data is updated infinitely. I can see the listview as well as the database keep on appending the data, and can only be stopped by closing the app.
I have tried to move SectionDetails newSD = new SectionDetails(code, seats) and updateRef.child(key).setValue(newSD) to outside for loop but the data doesn't get updated because the key is not passed to the path outside the for loop.
I haven't thoroughly looked at all the code you posted and may not understand your processing completely. There are two things that might be causing some of the problems you described.
In FirebaseHelper, method onChildChanged() calls fetchData(), which adds the changed section details to the array list. Shouldn't you be updating the existing section details instead of adding them again? Also, in onChildRemoved(), the section details are not removed from the array list. Don't they need to be removed?
In CustomAdapter the click listeners for your buttons add anonymous ValueEventListeners. Because they are anonymous, you have no way of removing them when they are no longer needed. ValueEventListeners added with addValueEventListener() remain active until removed. If your goal is to get the data once, use addListenerForSingleValueEvent().