Android Intent not transferring data to another activity? Firebase Data - java

I asked about this yesterday but never had much luck, I thought I would re-ask with a clearer outline of what I am trying to do..
Firstly, here is my Firebase hierarchy
I have a RecyclerViewAdapter/View which finds the Uid, and if the user has dogs listed on their 'account' then it will display a list of dogs, in this case finn and moo, this section is my first activity - ChooseDog. Each dog is clickable, and after clicking, it will take you to the second activity - DogProfile - where I am displaying details from each particular dog.
My problem is that the profile is only picking a random dog (if there is more than one) and displaying those details, I cannot seem to differentiate the results.
I want to take the name displayed on the card/button on ChooseDog, send it to the DogProfile via an intent, then use that name to show the correct details for the chosen dog. I'm quite new to this, but feel like I've done everything somewhat correctly? Any help is appreciated. I have been able to find the details when declaring the name explicitly, but not when trying to retrieve the name from the card.
ChooseDog (1st Activity)
TextView dogName;
databaseReference = FirebaseDatabase.getInstance().getReference("user").child(uid).child("dogs");
dogName = findViewById(R.id.dogName);
ItemClickSupport.addTo(recyclerView).setOnItemClickListener(
new ItemClickSupport.OnItemClickListener() {
#Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
getDogData();
Intent intent = new Intent(ChooseDog.this, DogProfile.class);
intent.putExtra("name", dogName.getText().toString());
startActivity(intent);
}
}
);
private void getDogData() {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String dogName = ds.child("name").getValue(String.class);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG", "onCancelled", databaseError.toException());
}
});
}
DogProfile (2nd Activity)
private void getDogData() {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("user").child(uid).child("dogs");
String dogsName = getIntent().getExtras().getString("name");
Query query = databaseReference.orderByChild("name").equalTo(dogsName);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String name = ds.child("name").getValue(String.class);
String dogBreed = ds.child("breed").getValue(String.class);
String dogAge = ds.child("age").getValue(String.class);
String gender = ds.child("gender").getValue(String.class);
String dogWeight = ds.child("weight").getValue(String.class);
String neutered = ds.child("neutered").getValue(String.class);
dogName.setText(name);
breed.setText(dogBreed);
age.setText(dogAge);
dogGender.setText(gender);
weight.setText(dogWeight);
isNeutered.setText(neutered);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG", "onCancelled", databaseError.toException());
}
});
}
Logcat
2021-07-29 10:13:49.990 17941-17941/com.example.doggo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.doggo, PID: 17941
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.example.doggo.ChooseDog$1.onItemClicked(ChooseDog.java:69)
at com.example.doggo.ItemClickSupport$1.onClick(ItemClickSupport.java:16)
at android.view.View.performClick(View.java:8160)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1139)

This answer based on your last few questions , hope this will help you. Today I will not talk about firebase authentication or store data part , because you already done that . I understand that at first you want to retrieve data and want show it in RecyclerView , then if user click on these specific data/item you want to show details through DogProfile. Almost of your code fine , in getDogData() method you should just add dogName with ArrayList and populate the Adapter . Then pass the data trough intent , use adapter.getItem(position) instead dogName variable.
Just follow these code-
ChooseDog class
public class ChooseDog extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
MyRecyclerViewAdapter adapter;
private DatabaseReference databaseReference;
private ArrayList<String> dogList;
private String dogName;
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_dog);
// dogName = findViewById(R.id.dogName);
databaseReference = FirebaseDatabase.getInstance().getReference("user").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("dogs");
getDogData();
dogList = new ArrayList<>();
recyclerView = findViewById(R.id.dogList);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
adapter = new MyRecyclerViewAdapter(getApplicationContext(), dogList);
adapter.setClickListener(this);
}
private void getDogData() {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
dogName = ds.child("name").getValue().toString();
dogList.add(dogName);
recyclerView.setAdapter(adapter);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG", "onCancelled", databaseError.toException());
}
});
}
#Override
public void onItemClick(View view, int position) {
Toast.makeText(this, "You Selected " + adapter.getItem(position), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(ChooseDog.this, DogProfile.class);
intent.putExtra("name", adapter.getItem(position));
startActivity(intent);
}
}
activity_choose_do.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ChooseDog">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/dogList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MyRecyclerViewAdapter class
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private List<String> zData;
private LayoutInflater zInflater;
private ItemClickListener mClickListener;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, List<String> data) {
this.zInflater = LayoutInflater.from(context);
this.zData = data;
}
// inflates the row layout from xml when needed
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = zInflater.inflate(R.layout.recyclerview_layout, parent, false);
return new ViewHolder(view);
}
// binds the data to the TextView in each row
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
String dog = zData.get(position);
holder.myTextView.setText(dog);
}
// total number of rows
#Override
public int getItemCount() {
return zData.size();
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.dogsName);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return zData.get(id);
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
recyclerview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<TextView
android:id="#+id/dogsName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"/>
DogProfile class , in this some wrong cast I was found , such as Spinner as TextView . Here fixed copy -
public class DogProfile extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private FirebaseUser currentUser;
TextView breed;
Spinner ageDropdown, genderDropdown, weightDropdown, neuteredDropdown; //zi
String uid;
String dogId;
private EditText dogName; //zi
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dog_profile);
firebaseAuth = FirebaseAuth.getInstance();
currentUser = firebaseAuth.getCurrentUser();
uid = currentUser.getUid();
dogName = findViewById(R.id.name);
breed = findViewById(R.id.breed);
ageDropdown = findViewById(R.id.age);
genderDropdown= findViewById(R.id.gender);
weightDropdown = findViewById(R.id.weight);
neuteredDropdown = findViewById(R.id.neutered);
getDogData();
}
private void getDogData() {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("user").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("dogs");
String dogsName = getIntent().getExtras().getString("name");
Query query = databaseReference.orderByChild("name").equalTo(dogsName);
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String name = (String) ds.child("name").getValue();
if (name.equals(dogsName)) {
String dogBreed = (String) ds.child("breed").getValue();
String dogAge = (String) ds.child("age").getValue();
String gender = (String) ds.child("gender").getValue();
String dogWeight = (String) ds.child("weight").getValue();
String neutered = (String) ds.child("neutered").getValue();
dogName.setText(name);
breed.setText(dogBreed);
ArrayAdapter<String> ageAdapter = new ArrayAdapter<String> (getApplicationContext(), android.R.layout.simple_spinner_item, Collections.singletonList(dogAge));
ageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ageDropdown.setAdapter(ageAdapter);
ArrayAdapter<String> weightAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, Collections.singletonList(dogWeight));
weightAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
weightDropdown.setAdapter(weightAdapter);
ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, Collections.singletonList(gender));
genderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
genderDropdown.setAdapter(genderAdapter);
ArrayAdapter<String> neuteredAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, Collections.singletonList(neutered));
neuteredAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
neuteredDropdown.setAdapter(neuteredAdapter);
} else {
Toast.makeText(DogProfile.this, "Please try again..", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
Log.w("TAG", "onCancelled", databaseError.toException());
}
});
}
}
dog_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DogProfile">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="560dp"
android:layout_marginStart="9dp"
android:layout_marginLeft="9dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Spinner
android:id="#+id/age"
android:layout_width="160dp"
android:layout_height="59dp"
android:autofillHints="age"
android:hint="Age"
android:inputType="text"
android:labelFor="#+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.085"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.353" />
<Spinner
android:id="#+id/weight"
android:layout_width="160dp"
android:layout_height="61dp"
android:autofillHints="weight"
android:hint="Weight (kg)"
android:inputType="text"
android:labelFor="#+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.901"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.356" />
<TextView
android:id="#+id/breed"
android:layout_width="347dp"
android:layout_height="57dp"
android:autofillHints="breed"
android:focusable="true"
android:hint="Breed"
android:inputType="text"
android:labelFor="#+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.434"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.196" />
<EditText
android:id="#+id/name"
android:layout_width="354dp"
android:layout_height="56dp"
android:autofillHints="Name"
android:hint="Name"
android:inputType="text"
android:labelFor="#+id/name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.032" />
<Spinner
android:id="#+id/gender"
android:layout_width="160dp"
android:layout_height="61dp"
android:autofillHints="gender"
android:hint="Gender"
android:inputType="text"
android:labelFor="#+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.086"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.532" />
<Spinner
android:id="#+id/neutered"
android:layout_width="160dp"
android:layout_height="56dp"
android:autofillHints="neutered"
android:hint="Neutered?"
android:inputType="text"
android:labelFor="#+id/name"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.901"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.535" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Daily Exercise Goal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.058"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.665" />
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.624" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Related

The function of adding a recipe to the database favorites does not work

I have a problem in the application, I want to make a function to add a recipe to my favorites, but there is a problem, everything seems to look fine, but when I click on the save button, I first compare the current icon with two icons selected and not selected, if selected, I delete a recipe from my favorite firebase database, and if it’s not selected, I add it, but there is a problem when I press my if else system for some reason starts in a loop and even the button seems to be pressed by itself I don’t know why this happens, can you please help, and i use RecyclerView.
My Recipe Item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:foregroundGravity="center"
app:cardCornerRadius="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical"
android:paddingBottom="30dp">
<ImageView
android:id="#+id/recipeImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/app_name"
android:maxHeight="260dp"
android:scaleType="fitXY"
android:transitionName="courseImage" />
<TextView
android:id="#+id/recipeTitle"
android:layout_width="223dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fontFamily="#font/baloo"
android:text="1234567890123456"
android:textColor="#color/black"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="52dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:fontFamily="#font/baloo"
android:text="By"
android:textColor="#color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:fontFamily="#font/baloo"
android:text="Username"
android:textColor="#color/textColor"
android:textSize="18sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:contentDescription="TODO"
app:srcCompat="#drawable/ic_time_svgrepo_com" />
</LinearLayout>
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/baloo"
android:text="time"
android:textColor="#color/black" />
</LinearLayout>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/saveBtn"
android:layout_width="31dp"
android:layout_height="36dp"
android:layout_marginLeft="190dp"
android:layout_weight="1"
app:srcCompat="#drawable/save" />
</androidx.appcompat.widget.LinearLayoutCompat>
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
My Adapter:
public class RecipeAdapter extends RecyclerView.Adapter<RecipeAdapter.RecipeViewHolder> {
FirebaseAuth mAuth;
FirebaseUser mUser;
DatabaseReference mRef;
DatabaseReference mRef2;
DatabaseReference mRef3;
DatabaseReference mRef4;
Context context;
public List<Recipe> recipes;
public RecipeAdapter(Context context, List<Recipe> courses) {
this.context = context;
this.recipes = courses;
}
#NonNull
#Override
public RecipeViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View recipeItems = LayoutInflater.from(context).inflate(R.layout.recipe_item, parent, false);
return new RecipeAdapter.RecipeViewHolder(recipeItems);
}
#Override
public void onBindViewHolder(#NonNull RecipeViewHolder holder, #SuppressLint("RecyclerView") int position) {
mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
mRef = FirebaseDatabase.getInstance().getReference().child("users");
mRef2 = FirebaseDatabase.getInstance().getReference().child("count");
mRef4 = FirebaseDatabase.getInstance().getReference().child("count2").child(mUser.getUid());
mRef3 = FirebaseDatabase.getInstance().getReference().child("favorite").child(mUser.getUid());
//Listener for favorite Btn
holder.saveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mRef4.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot1) {
try {
if (holder.saveBtn.getDrawable().getConstantState().equals(context.getDrawable(R.drawable.save).getConstantState())) {
System.out.println("g");
mRef3.child(recipes.get(position).id).setValue("");
mRef4.setValue(Integer.parseInt(snapshot1.getValue().toString()) + 1);
holder.saveBtn.setImageResource(R.drawable.save2);
} else {
System.out.println("b");
mRef3.child(recipes.get(position).id).removeValue();
holder.saveBtn.setImageResource(R.drawable.save);
}
return;
} catch (IndexOutOfBoundsException e) {
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
});
mRef4.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot1) {
System.out.println(snapshot1.getValue().toString());
mRef3.child(recipes.get(position).id).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.getValue() == null) {
} else {
holder.saveBtn.setImageResource(R.drawable.save2);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, RecipePage.class);
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(
(Activity) context,
new Pair<View, String>(holder.recipeImage, "recipeImage")
);
intent.putExtra("recipeImage", recipes.get(position).getPhotoLink());
intent.putExtra("recipeTitle", recipes.get(position).getTitle());
intent.putExtra("recipeCookTime", recipes.get(position).getCookTime());
intent.putExtra("recipeServes", recipes.get(position).getServes());
intent.putExtra("recipePrepTime", recipes.get(position).getPrepTime());
intent.putExtra("recipeAccess", recipes.get(position).getAccess());
intent.putExtra("recipeDesc", recipes.get(position).getDescription());
intent.putExtra("recipeIngr", recipes.get(position).getIngredients());
intent.putExtra("recipeDire", recipes.get(position).getDirections());
context.startActivity(intent, options.toBundle());
}
});
}
#Override
public int getItemCount() {
return recipes.size();
}
public static final class RecipeViewHolder extends RecyclerView.ViewHolder {
public ImageView recipeImage, saveBtn;
TextView username, time, title;
public RecipeViewHolder(#NonNull View itemView) {
super(itemView);
saveBtn = itemView.findViewById(R.id.saveBtn);
recipeImage = itemView.findViewById(R.id.recipeImage);
username = itemView.findViewById(R.id.username);
username.setPaintFlags(username.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
time = itemView.findViewById(R.id.time);
title = itemView.findViewById(R.id.recipeTitle);
}
}
}
The problem is in this sort of code:
mRef4.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot1) {
try {
if (holder.saveBtn.getDrawable().getConstantState().equals(context.getDrawable(R.drawable.save).getConstantState())) {
System.out.println("g");
mRef3.child(recipes.get(position).id).setValue("");
mRef4.setValue(Integer.parseInt(snapshot1.getValue().toString()) + 1);
holder.saveBtn.setImageResource(R.drawable.save2);
} else {
System.out.println("b");
mRef3.child(recipes.get(position).id).removeValue();
holder.saveBtn.setImageResource(R.drawable.save);
}
return;
} catch (IndexOutOfBoundsException e) {
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
You're adding a value listener to mRef4, which means that your onDataChange gets called immediately for the current value of mRef4 and then **every time mRef4 is modified tooSince you modify mRef4 inside onDataChange, you're causing an infinite loop.
Most likely you want to use addListenerForSingleValueEvent instead of addValueEventListener:
// 👇
mRef4.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot1) {
try {
if (holder.saveBtn.getDrawable().getConstantState().equals(context.getDrawable(R.drawable.save).getConstantState())) {
System.out.println("g");
mRef3.child(recipes.get(position).id).setValue("");
mRef4.setValue(Integer.parseInt(snapshot1.getValue().toString()) + 1);
holder.saveBtn.setImageResource(R.drawable.save2);
} else {
System.out.println("b");
mRef3.child(recipes.get(position).id).removeValue();
holder.saveBtn.setImageResource(R.drawable.save);
}
return;
} catch (IndexOutOfBoundsException e) {
throw RuntimeError(e); // 👈 never ignore errors
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException(); // 👈 never ignore errors
}
});
You might have more such instances in your code, so check for any addValueEventListener and wonder if you indeed want to listen for updates or only want to get the value once.

Recyclerview and adapter doesn’t show

https://www.youtube.com/watch?v=M8sKwoVjqU0
i watched this video and i have done evertyhing same with.But there is a problem i guess myadapter class doesn't work because when i click button it comes just white screen as you see on the picture. i couldn't figure out where is the fault when i check the log no problem. but i just wanna take data from firebase to textview. but i can't get any result also at least it must see there just textview which is have text firstname but i can't see on my activity.i might be don't know the true page to will show
---this my myadapter.class----
public class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder> {
Context context;
ArrayList<User> list ;
public Myadapter(Context context, ArrayList<User> list) {
this.context = context;
this.list = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
return new MyViewHolder(v) ;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
User user =list.get(position);
holder.textView.setText(user.getTitle());
}
#Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView textView, TextView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
TextView = itemView.findViewById(R.id.txtview);
textView=itemView.findViewById(R.id.title);
}
}
---this my userlist.class---
public class userlist extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference database;
Myadapter myadapter;
ArrayList<User>list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userlist);
recyclerView=findViewById(R.id.userlist);
database = FirebaseDatabase.getInstance().getReference("users");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
myadapter = new Myadapter(this,list);
recyclerView.setAdapter(myadapter);
database.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
for ( DataSnapshot dataSnapshot: snapshot.getChildren()){
User user=dataSnapshot.getValue(User.class);
list.add(user);
}
myadapter.notifyDataSetChanged();
}
---this my mainactivity.class---
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this,userlist.class);
startActivity(i);
finish();}
});
}
}
{
"users" : {
"value1" : {
"title" : "reachthis"
}
}
}
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".userlist">
---activity_userlist---
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/userlist"
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>```
---item.xml---
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardElevation="8dp"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"> </LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
>
<TextView
android:id="#+id/txtview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first name"
android:textColor="#color/black"
android:textSize="26sp"
android:textStyle="bold"></TextView><TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first name"
android:textColor="#color/black"
android:textSize="20sp"
></TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>

How to refer FIrebase Child in Child record in Firebase Recycler adapter to show in recyclerview

I have written code to list contacts in recycler view. But it doesn't display value in recycler view. The code is given below.
HomeActivity.java
private DatabaseReference mDatabase;
private RecyclerView PList;
private FirebaseRecyclerAdapter<project, ProjViewHolder> FBRA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
PList = findViewById(R.id.ProjList);
PList.setHasFixedSize(true);
PList.setLayoutManager(new LinearLayoutManager(this));
mDatabase = FirebaseDatabase.getInstance().getReference().child("Contacts");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<project> options = new FirebaseRecyclerOptions.Builder<project>().setQuery(mDatabase, project.class).build();
FBRA = new FirebaseRecyclerAdapter<project, ProjViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull ProjViewHolder holder, int position, #NonNull project model) {
String proj_key = getRef(position).getKey();
holder.cpname.setText(model.getCname());
holder.cplocation.setText(model.getCcity());
holder.ccname.setText(model.getPcontact());
holder.ccdesig.setText(model.getPdesig());
}
#Override
public ProjViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.proj_row, parent, false);
return new ProjViewHolder(view);
}
};
PList.setAdapter(FBRA);
FBRA.startListening();
}
public class ProjViewHolder extends RecyclerView.ViewHolder {
TextView cpname, cplocation, ccname, ccdesig;
ProjViewHolder(#NonNull View itemView) {
super(itemView);
cpname = itemView.findViewById(R.id.FCName);
cplocation = itemView.findViewById(R.id.FCLocation);
ccname = itemView.findViewById(R.id.FPName);
ccdesig = itemView.findViewById(R.id.FPDesig);
}
}
#Override
protected void onStop() {
super.onStop();
FBRA.stopListening();
}
FormActivity.java
public class FormActivity extends AppCompatActivity {
private EditText CName, PContact,PDesig, CCity;
private FirebaseDatabase mDatabase;
private Button Fbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
CName = findViewById(R.id.CompName);
PContact = findViewById(R.id.PContact);
PDesig = findViewById(R.id.PDesig);
CCity = findViewById(R.id.CCity);
mDatabase = FirebaseDatabase.getInstance();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.save, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
String dCName = CName.getText().toString().trim();
String dPContact = PContact.getText().toString().trim();
String dPDesig = PDesig.getText().toString().trim();
String dCCity = CCity.getText().toString().trim();
String name = CName.getText().toString().trim();
DatabaseReference mDbRef = mDatabase.getReference().child("Contacts").child(name).push();
mDbRef.child("cname").setValue(dCName);
mDbRef.child("pcontact").setValue(dPContact);
mDbRef.child("pdesig").setValue(dPDesig);
mDbRef.child("ccity").setValue(dCCity);
Toast.makeText(FormActivity.this, "Contact Submitted Successfully", Toast.LENGTH_LONG).show();
Intent intent = new Intent(FormActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
}
return super.onOptionsItemSelected(item);
}
}
Project.class
public class project {
String cname, pcontact, pdesig, ccity;
public project(){
}
public project(String cname, String pcontact, String pdesig, String ccity) {
this.cname = cname;
this.pcontact = pcontact;
this.pdesig = pdesig;
this.ccity = ccity;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getPcontact() {
return pcontact;
}
public void setPcontact(String pcontact) {
this.pcontact = pcontact;
}
public String getPdesig() {
return pdesig;
}
public void setPdesig(String pdesig) {
this.pdesig = pdesig;
}
public String getCcity() {
return ccity;
}
public void setCcity(String ccity) {
this.ccity = ccity;
}
}
HomeActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeActivity">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:id="#+id/ProjList"
/>
</RelativeLayout>
ProjRow.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/FCName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:elegantTextHeight="true"
android:textColor="#color/colorBlue"
android:maxLength="25"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="16dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
/>
<TextView
android:id="#+id/FCLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:elegantTextHeight="true"
android:textColor="#color/colorDark"
android:layout_marginTop="13dp"
android:paddingTop="5dp"
android:textSize="14dp"
/>
<TextView
android:id="#+id/FPName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FCName"
android:layout_alignParentLeft="true"
android:elegantTextHeight="true"
android:textStyle="bold"
android:textColor="#color/colorText"
android:paddingTop="5dp"
android:paddingRight="3dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="15dp"
/>
<TextView
android:id="#+id/FPDesig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/FCName"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:padding="5dp"
android:textSize="14dp"
android:textColor="#color/colorNext"
android:elegantTextHeight="true"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
the code is working fine without error. But the intended value is not captured from database. If push() is removed from DatabaseReference mDbRef = mDatabase.getReference().child("Contacts").child(name).push(); , it works absolutely fine. but it overwrites on existing data while adding new data for the same 'name'. After adding push, the data are getting stored in sub child record. Please guide me how to refer and get back the data in recycleview adapter.
The each record stored under 'Contacts' before adding push()but overwrites when added a person for the same company.
After adding push() each record stored under company name with unique key.

Can someone help me understand what the problem is? its not showing the data correctly in the RecyclerView

when the app is run everything is fine except that the data (Text) does not show how it's supposed to.
It was working fine when the first run the app with this code but when I was making amendments to the code it somehow stopped displaying the data the I way I wanted and I can't find out why this is happening. I even undid all the amendments but it still will not work.
This is my Main just in case
public class MainActivity extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("DefaultWords");
EditText engText, araText;
Button submitBtn, display, deletebtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writeToData();
disButton();
}
private void writeToData() {
engText = findViewById(R.id.english);
araText = findViewById(R.id.arabic);
submitBtn = findViewById(R.id.button);
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String engWord = engText.getText().toString().trim();
String araWord = araText.getText().toString().trim();
MyWords words = new MyWords(engWord, araWord);
myRef.child(engWord).setValue(words);
}
});
}
}
My Adapter
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private LayoutInflater layoutInflater;
private List<MyWords> words;
public Adapter(Context context, ArrayList<MyWords> words) {
this.layoutInflater = LayoutInflater.from(context);
this.words = words;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.new_custom_cards, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
MyWords words1 = words.get(position);
holder.englishText.setText(words1.getEnglish());
holder.arabicText.setText(words1.getArabic());
}
#Override
public int getItemCount() {
return words.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView englishText, arabicText;
ViewHolder(#NonNull View itemView) {
super(itemView);
englishText = itemView.findViewById(R.id.cardTextView);
arabicText = itemView.findViewById(R.id.cardTextView2);
}
}
my RecyclerView
public class RecylePractice extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("DefaultWords");
RecyclerView recyclerView;
Adapter adapter;
ArrayList<MyWords> wordsArrayList = new ArrayList<>();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_recyle_view);
recyclerView = findViewById(R.id.recyle);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new Adapter(this, wordsArrayList);
recyclerView.setAdapter(adapter);
recylerReadData();
}
private void recylerReadData() {
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
MyWords words = postSnapshot.getValue(MyWords.class);
wordsArrayList.add(words);
adapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Log.d("TAG", databaseError.getMessage());
}
});
}
}
this is what happen the display is run
I wanted the English words to display on the left and the Arabic words to display on the right.
my new_custom_cards xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCDFA6A6"
android:padding="10dp"
>
<androidx.cardview.widget.CardView
android:id="#+id/cardView_custom"
android:layout_width="match_parent"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.13">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<TextView
android:id="#+id/cardTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:ellipsize="end"
android:maxLines="1"
android:text="#string/english"
android:textAlignment="viewStart"
android:textColor="#color/red"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/cardTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ellipsize="end"
android:maxLines="1"
android:text="#string/arabic"
android:textAlignment="viewStart"
android:textColor="#color/red"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/eqauls123"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/cardTextView2"
app:layout_constraintStart_toEndOf="#+id/cardTextView"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
You set wrap_content as layout_width in your RecyclerView. Try to change it match_parent like below
android:layout_width="match_parent"
You did set wrap_content in your itemview layout width.
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
MyWords words = postSnapshot.getValue(MyWords.class);
wordsArrayList.add(words);
adapter.notifyDataSetChanged();
}
}
REPLACE ABOVE CODE WITH BELOW CODE
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
MyWords words = postSnapshot.getValue(MyWords.class);
wordsArrayList.add(words);
}
adapter.notifyDataSetChanged();
}

I cant get all the data from firebase to my Listview

I couldnt able to display my data from firebase to my listview in android application. Rather it only shows one data.
Heres the current data of my database:
Database
And this is my java:
public class scheduleList extends ArrayAdapter<Schedule> {
private Activity context;
private List<Schedule> scheduleList;
public scheduleList(Activity context, List<Schedule> scheduleList){
super(context, R.layout.listlayout, scheduleList);
this.context=context;
this.scheduleList=scheduleList;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listviewItem = inflater.inflate(R.layout.listlayout, null, true);
TextView textViewName = (TextView) listviewItem.findViewById(R.id.textViewName);
TextView textViewSchedule = (TextView) listviewItem.findViewById(R.id.textViewSchedule);
Schedule schedule = scheduleList.get(position);
textViewName.setText(schedule.getAppointer());
textViewSchedule.setText(schedule.getAppointment_schedule());
return listviewItem;
}}
My java file:
public class Appointment extends AppCompatActivity {
CalendarView calendarView;
TextView myDate;
private Button btn1;
ListView listViewSchedule;
List<Schedule> scheduleList;
DatabaseReference databaseAppointments;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_appointment);
databaseAppointments = FirebaseDatabase.getInstance().getReference("appointment");
calendarView = (CalendarView) findViewById(R.id.calendarView);
myDate = (TextView) findViewById(R.id.myDate);
listViewSchedule=(ListView) findViewById(R.id.listViewSchedule);
scheduleList = new ArrayList<>();
btn1 = (Button) findViewById(R.id.appt);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Appointment.this, appointments2.class);
Appointment.this.startActivity(myIntent);
}
});
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(#NonNull CalendarView view, int i, int i1, int i2) {
String date = (i1 + 1) + "/" + i2 + "/" + i;
myDate.setText(date);
}
});
}
#Override
protected void onStart() {
super.onStart();
databaseAppointments.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
scheduleList.clear();
for(DataSnapshot scheduleSnapshot: dataSnapshot.getChildren()){
Schedule schedule = scheduleSnapshot.getValue(Schedule.class);
scheduleList.add(schedule);
}
scheduleList adapter = new scheduleList(Appointment.this, scheduleList);
listViewSchedule.setAdapter(adapter);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}}
and lastly my xml file containing the listview:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cs409.instappoint.Appointment">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/myDate"
android:textSize="23sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select the date"
android:textColor="#ee912a"
android:textAlignment="center"
android:layout_marginStart="125dp"
android:layout_marginTop="48dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="380dp"
android:layout_marginStart="10dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<CalendarView
android:id="#+id/calendarView"
android:layout_width="wrap_content"
android:layout_height="320dp">
</CalendarView>
</LinearLayout>
<View
android:layout_width="500dp"
android:layout_height="1dp"
android:layout_gravity="center"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="0dp"
android:background="#ee912a" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Appointments"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"/>
<ListView
android:id="#+id/listViewSchedule"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/appt"
android:layout_width="450dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="120dp"
android:layout_marginStart="120dp"
android:layout_marginTop="130dp"
android:background="#drawable/bg_calen"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Appoint now"
android:textColor="#fff" />
</LinearLayout>
Im guessing that the scrollview layout affects the listview but im not sure. Also the listview display whatevers the first on the list.
use FirebaseRecyclerAdapter. search example of how to use FirebaseRecylerAdapter with example.

Categories

Resources