0

I would like to know how to structure my data in Firebase database so it return a List<String> when queried. I'm implementing a image slider, I need Firebase to return a List containing images url that my model class which implement Parcelable can parse.

Here is my model class

public class Property implements Parcelable {
private int price;
private String address;
private int numberOfBed;
private int numberOfBath;
private int numberOfCar;
private List<String> propertyImage= new ArrayList<>();
private float lotDim;

public Property() { } //Needed for Firebase's auto data mapping

public Property(int price, String address, int numberOfBed, int numberOfBath,
                int numberOfCar, List<String> propertyImage, float lotDim) {
    this.price = price;
    this.address = address;
    this.numberOfBed = numberOfBed;
    this.numberOfBath = numberOfBath;
    this.numberOfCar = numberOfCar;
    this.propertyImage = propertyImage;
    this.lotDim = lotDim;
}

protected Property(Parcel in) {
    price = in.readInt();
    address = in.readString();
    numberOfBed=in.readInt();
    numberOfBath = in.readInt();
    numberOfCar = in.readInt();
    in.readStringList(propertyImage);
    lotDim = in.readFloat();
}

public static  final Creator<Property> CREATOR = new Creator<Property>() {
    @Override
    public Property createFromParcel(Parcel in) {
        return new Property(in);
    }

    @Override
    public Property[] newArray(int i) {
        return new Property[i];
    }
};

public int getPrice() {
    return price;
}
public void setPrice(int price) {
    this.price = price;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public int getNumberOfBed() {
    return numberOfBed;
}

public void setNumberOfBed(int numberOfBed) {
    this.numberOfBed = numberOfBed;
}

public int getNumberOfBath() {
    return numberOfBath;
}

public void setNumberOfBath(int numberOfBath) {
    this.numberOfBath = numberOfBath;
}

public int getNumberOfCar() {
    return numberOfCar;
}

public void setNumberOfCar(int numberOfCar) {
    this.numberOfCar = numberOfCar;
}

public List<String> getPropertyImage() {
    return propertyImage;
}

public void setPropertyImage(List<String> propertyImage) {
    this.propertyImage = propertyImage;
}

public float getLotDim() {
    return lotDim;
}

public void setLotDim(float lotDim) {
    this.lotDim = lotDim;
}


@Override
public  int describeContents(){
    return  0;
}

@Override
public void writeToParcel( Parcel dest, int flags){
    dest.writeInt(price);
    dest.writeString(address);
    dest.writeInt(numberOfBed);
    dest.writeInt(numberOfBath);
    dest.writeInt(numberOfCar);
    dest.writeStringList(propertyImage);
    dest.writeFloat(lotDim);
}

}

Here is my current Firebase structure. it return a String actually. I want it to return a List of propertyImage. enter image description here

This is the fragment that will use the model class, That's where I need to implement the code. Notice at the bottom, I work out something, in the firebase database I appended all the urls in one string(well it's a dirty solution), and split the url turning that String into an array. :-) So how to implement the new code provided by @Alex Mano. thanks guys

package com.realty.drake.kunuk;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.text.NumberFormat;
import java.util.Locale;


/**
* Created by drake on 4/11/18
 */

public class Tab1Buy extends Fragment {
private DatabaseReference propertyRef;
private RecyclerView mPropertyRecyclerView;
FirebaseRecyclerAdapter<Property, PropertyViewHolder> mPropertyAdapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.property_tab, container, false);
    mPropertyRecyclerView = rootView.findViewById(R.id.property_recyclerView);
    return rootView;
}


//TODO Check internet and display error msg if internet down
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mPropertyRecyclerView.hasFixedSize();
    mPropertyRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    final ProgressBar progressBar = view.findViewById(R.id.progressBar);
    progressBar.setVisibility(View.VISIBLE);

    propertyRef = FirebaseDatabase.getInstance()
            .getReference()
            .child("Buy");
    propertyRef.keepSynced(true);

    // keyQuery - the Firebase location containing the list of keys to be found in dataRef
    //Query personQuery = personRef.orderByKey();


    FirebaseRecyclerOptions<Property> options =
            new FirebaseRecyclerOptions.Builder<Property>()
                    .setQuery(propertyRef, Property.class)
                    .build();

    mPropertyAdapter = new FirebaseRecyclerAdapter<Property, PropertyViewHolder>(options) {


        @Override
        // Bind the Property object to the ViewHolder PropertyHolder
        public void onBindViewHolder(@NonNull PropertyViewHolder holder,
                                     final int position, @NonNull final Property model) {
            holder.setPrice(model.getPrice());
            holder.setAddress(model.getAddress());
            holder.setNumberOfBed(model.getNumberOfBed());
            holder.setNumberOfBath(model.getNumberOfBath());
            holder.setNumberOfCar(model.getNumberOfCar());
            holder.setPropertyImage(model.getPropertyImage());

        //Intent send Parcelable to PropertyDetail
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().startActivity(new Intent(getActivity(), PropertyDetail.class)
                .putExtra("Property", model));

            }
        });


        }

        @Override
        public PropertyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new instance of the ViewHolder, in this case we are using a custom
            // layout called R.layout.property_card for each item
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.property_card, parent, false);
            return new PropertyViewHolder(view);

        }

        @Override
        public void onDataChanged() {
            // Called each time there is a new data snapshot. You may want to use this method
            // to hide a loading spinner or check for the "no documents" state and update your UI.
            // ...
            progressBar.setVisibility(View.GONE);
        }

        //TODO Implement onError
        @Override
        public void onError(@NonNull DatabaseError e) {
            // Called when there is an error getting data. You may want to update
            // your UI to display an error message to the user.
            // ...
            progressBar.setVisibility(View.GONE);
            Toast.makeText(getActivity(), "DatabaseError", Toast.LENGTH_SHORT).show();
        }

    };
    mPropertyRecyclerView.setAdapter(mPropertyAdapter);



}

@Override
public void onStart() {
    super.onStart();
    mPropertyAdapter.startListening();
}

@Override
public void onStop() {
    super.onStop();
    mPropertyAdapter.stopListening();
}


public class PropertyViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public PropertyViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setPrice(int price) {
        String currencyPrice = NumberFormat //Format the price variable in currency form
                .getCurrencyInstance(Locale.US)
                .format(price);
        TextView Price = mView.findViewById(R.id.post_price);
        Price.setText(currencyPrice);
    }

    public void setAddress(String address){
        TextView Address = mView.findViewById(R.id.post_address);
        Address.setText(String.valueOf(address));
    }

    public void setNumberOfBed(int numberOfBed){
        TextView NumberOfBed = mView.findViewById(R.id.post_bedroom);
        NumberOfBed.setText(String.valueOf(numberOfBed));
    }

    public void setNumberOfBath(int numberOfBath){
        TextView NumberOfBath = mView.findViewById(R.id.post_bathroom);
        NumberOfBath.setText(String.valueOf(numberOfBath));
    }

    public  void  setNumberOfCar(int numberOfCar) {
        TextView NumberOfCar = mView.findViewById(R.id.post_garage);
        NumberOfCar.setText(String.valueOf(numberOfCar));
    }

    public void setPropertyImage(String propertyImage){
        ImageView imageView = mView.findViewById(R.id.post_propertyImage);

        //take one long string containing multiple url in and parse it
        String propertyImageArray[] = propertyImage.split(",");

        //TODO add loading icon for placeholder

        // Download directly from StorageReference using Glide
        // (See MyAppGlideModule for Loader registration)
        GlideApp.with(getContext())
                .load(propertyImageArray[0])
                .fitCenter()
                .into(imageView);
    }
}

}
2

1 Answer 1

1

Assuming that the Buy node is a direct child of your Firebase root, to solve this, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference propertyImageRef = rootRef.child("Buy").child("1").child("propertyImage");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String url = ds.getValue(String.class);
            Log.d("TAG", url);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
propertyImageRef.addListenerForSingleValueEvent(valueEventListener);

The output will be all those urls.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @Alex Mamo, I will test it and report back.
That's perfect. Keep me posted.
The code is a bit ambiguous for me. Please excuse my noobness. Will the 'for' loop append url or overwrite the previous value. I mean at the end of the loop, will the String url contains all the urls? Actually I'm using firebase-ui database, and the code you sent need to be uploaded inside a fragment.I will upload the code
For the moment is just adding as a new line in the logcat containing each url. No, at the end it won't be a String with all urls. All urls are separate urls. If you want, you can add them to list if you want.
Can you please take a look at the code I just uploaded?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.