Skip to content Skip to sidebar Skip to footer

Android Intent Not Transferring Data To Another Activity? Firebase Data

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 R

Solution 1:

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-

  1. ChooseDog class

    publicclassChooseDogextendsAppCompatActivityimplementsMyRecyclerViewAdapter.ItemClickListener {
    
     MyRecyclerViewAdapter adapter;
     privateDatabaseReference databaseReference;
     privateArrayList<String> dogList;
     privateString dogName;
     privateRecyclerView recyclerView;
    
     @OverrideprotectedvoidonCreate(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 = newArrayList<>();
         recyclerView = findViewById(R.id.dogList);
         recyclerView.setLayoutManager(newLinearLayoutManager(getApplicationContext()));
         adapter = newMyRecyclerViewAdapter(getApplicationContext(), dogList);
         adapter.setClickListener(this);
    
    
     }
    
    
     privatevoidgetDogData() {
         databaseReference.addValueEventListener(newValueEventListener() {
             @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
    
                 for (DataSnapshot ds : dataSnapshot.getChildren()) {
    
                     dogName = ds.child("name").getValue().toString();
                     dogList.add(dogName);
                     recyclerView.setAdapter(adapter);
    
    
                 }
    
             }
    
             @OverridepublicvoidonCancelled(DatabaseError databaseError) {
                 Log.w("TAG", "onCancelled", databaseError.toException());
             }
         });
     }
    
     @OverridepublicvoidonItemClick(View view, int position) {
         Toast.makeText(this, "You Selected " + adapter.getItem(position), Toast.LENGTH_SHORT).show();
         Intent intent = newIntent(ChooseDog.this, DogProfile.class);
         intent.putExtra("name", adapter.getItem(position));
         startActivity(intent);
     }
    }
    
  2. activity_choose_do.xml

    <?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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.RecyclerViewandroid:id="@+id/dogList"android:layout_width="match_parent"android:layout_height="match_parent" /></androidx.constraintlayout.widget.ConstraintLayout>
  3. MyRecyclerViewAdapter class

    publicclassMyRecyclerViewAdapterextendsRecyclerView.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@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         Viewview= zInflater.inflate(R.layout.recyclerview_layout, parent, false);
         returnnewViewHolder(view);
     }
    
     // binds the data to the TextView in each row@OverridepublicvoidonBindViewHolder(ViewHolder holder, int position) {
         Stringdog= zData.get(position);
         holder.myTextView.setText(dog);
     }
    
     // total number of rows@OverridepublicintgetItemCount() {
         return zData.size();
     }
    
    
     // stores and recycles views as they are scrolled off screenpublicclassViewHolderextendsRecyclerView.ViewHolder implementsView.OnClickListener {
         TextView myTextView;
    
         ViewHolder(View itemView) {
             super(itemView);
             myTextView = itemView.findViewById(R.id.dogsName);
             itemView.setOnClickListener(this);
         }
    
         @OverridepublicvoidonClick(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 caughtvoidsetClickListener(ItemClickListener itemClickListener) {
         this.mClickListener = itemClickListener;
     }
    
     // parent activity will implement this method to respond to click eventspublicinterfaceItemClickListener {
         voidonItemClick(View view, int position);
     }
    }
    
  4. recyclerview_layout.xml

    <?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="10dp"><TextViewandroid:id="@+id/dogsName"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20sp"/>
  5. DogProfile class , in this some wrong cast I was found , such as Spinner as TextView . Here fixed copy -

    publicclassDogProfileextendsAppCompatActivity {
    
     private FirebaseAuth firebaseAuth;
     private FirebaseUser currentUser;
    
     TextView breed;
     Spinner ageDropdown, genderDropdown, weightDropdown, neuteredDropdown; //zi
     String uid;
     String dogId;
     private EditText dogName; //zi@OverrideprotectedvoidonCreate(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();
    
     }
    
     privatevoidgetDogData() {
    
         DatabaseReferencedatabaseReference= FirebaseDatabase.getInstance().getReference("user").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("dogs");
    
         StringdogsName= getIntent().getExtras().getString("name");
         Queryquery= databaseReference.orderByChild("name").equalTo(dogsName);
    
         query.addValueEventListener(newValueEventListener() {
    
             @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
    
                 for (DataSnapshot ds : dataSnapshot.getChildren()) {
                     Stringname= (String) ds.child("name").getValue();
    
                     if (name.equals(dogsName)) {
                         StringdogBreed= (String) ds.child("breed").getValue();
                         StringdogAge= (String) ds.child("age").getValue();
                         Stringgender= (String) ds.child("gender").getValue();
                         StringdogWeight= (String) ds.child("weight").getValue();
                         Stringneutered= (String) ds.child("neutered").getValue();
    
                         dogName.setText(name);
                         breed.setText(dogBreed);
    
    
         ArrayAdapter<String> ageAdapter = newArrayAdapter<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 = newArrayAdapter<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 = newArrayAdapter<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 = newArrayAdapter<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();
                     }
                 }
             }
    
             @OverridepublicvoidonCancelled(DatabaseError databaseError) {
                 Log.w("TAG", "onCancelled", databaseError.toException());
             }
         });
     }
    
    
    }
    
  6. dog_profile.xml

    <?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns: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.ConstraintLayoutandroid: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"><Spinnerandroid: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" /><Spinnerandroid: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" /><TextViewandroid: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" /><EditTextandroid: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" /><Spinnerandroid: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" /><Spinnerandroid: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" /><TextViewandroid: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" /><Viewandroid: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>

enter image description hereenter image description here

Post a Comment for "Android Intent Not Transferring Data To Another Activity? Firebase Data"