简体   繁体   中英

How to retrieve array data from children node in Firebase Realtime Database Java? (Firebase Recycler Adapter)

I knew my question is similar with the other questioners but it was not solved my problem because I am still curious about how to declare the item which is the children of parent node. This is my stucture data on my Firebase:

在此处输入图片说明

I've tried to created the classes for my case:

DolanIemClass.java

    private String name_tourism;
    private String location_tourism;
    private String info_tourism;
    private String telepon;
    private String url_photo;
    private String url_photo_collage;
    private double lat_location_tourism;
    private double lng_location_tourism;

    public DolanItemClass() {
        //constructor untuk panggilan ke DataSnapshot.getValue
    }

    public DolanItemClass(String name_tourism, String location_tourism, String info_tourism, String telepon, String url_photo, String url_photo_collage, double lat_location_tourism, double lng_location_tourism) {
        this.name_tourism = name_tourism;
        this.location_tourism = location_tourism;
        this.info_tourism = info_tourism;
        this.telepon = telepon;
        this.url_photo = url_photo;
        this.url_photo_collage = url_photo_collage;
        this.lat_location_tourism = lat_location_tourism;
        this.lng_location_tourism = lng_location_tourism;
    }

    public String getUrl_photo_collage() {
        return url_photo_collage;
    }

    public String getName_tourism() {
        return name_tourism;
    }

    public void setName_tourism(String name_tourism) {
        this.name_tourism = name_tourism;
    }

    public String getLocation_tourism() {
        return location_tourism;
    }

    public void setLocation_tourism(String location_tourism) {
        this.location_tourism = location_tourism;
    }

    public String getInfo_tourism() {
        return info_tourism;
    }

    public void setInfo_tourism(String info_tourism) {
        this.info_tourism = info_tourism;
    }

    public String getTelepon() {
        return telepon;
    }

    public void setTelepon(String telepon) {
        this.telepon = telepon;
    }

    public String getUrl_photo() {
        return url_photo;
    }

    public void setUrl_photo(String url_photo) {
        this.url_photo = url_photo;
    }

    public double getLat_location_tourism() {
        return lat_location_tourism;
    }

    public void setLat_location_tourism(double lat_location_tourism) {
        this.lat_location_tourism = lat_location_tourism;
    }

    public double getLng_location_tourism() {
        return lng_location_tourism;
    }

    public void setLng_location_tourism(double lng_location_tourism) {
        this.lng_location_tourism = lng_location_tourism;
    }
}

DolanViewHolder.class


import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

public class DolanImageViewHolder extends RecyclerView.ViewHolder {
    private final ImageView imgDetailData;

    public DolanImageViewHolder(@NonNull View itemView) {
        super(itemView);
        imgDetailData = itemView.findViewById(R.id.img_detail_item_data);
    }
    public void showImageArray(DolanItemClass dolanItemClass){
        Glide.with(itemView.getContext()).load(dolanItemClass.getUrl_photo_collage()).into(imgDetailData);
    }
}

And the activity class to show the list

package co.id.roningrum.firebasearrayimage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

public class DetailTouristListAct extends AppCompatActivity {
    private ImageView imgDetailData;
    private TextView tvNamaDetail, tvAlamatDetail, tvDescDetail;
    private RecyclerView rvImageDetailCollages;

    private DatabaseReference databaseReference;
    private ValueEventListener valueEventListener;
    private FirebaseRecyclerAdapter<DolanItemClass, DolanImageViewHolder> firebaseRecyclerAdapter;
    public static final String EXTRA_WISATA_KEY = "tourist_key";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_tourist_list);

        String touristKey = getIntent().getStringExtra(EXTRA_WISATA_KEY);
        if(touristKey == null){
            throw new IllegalArgumentException("Must pass Extra");
        }

        imgDetailData = findViewById(R.id.img_detail_data);
        tvNamaDetail = findViewById(R.id.tv_name_detail);
        tvAlamatDetail = findViewById(R.id.tv_info_tourism_detail);
        tvDescDetail = findViewById(R.id.tv_address_detail);
        rvImageDetailCollages = findViewById(R.id.rv_photo_collage);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("Tourism").child(touristKey);

        rvImageDetailCollages.setLayoutManager(new GridLayoutManager(this, 2));
        LoadDetailData();

    }

    private void ShowCollage() {
        Query query = databaseReference.child("url_photo_collage");
        FirebaseRecyclerOptions<DolanItemClass> options = new FirebaseRecyclerOptions.Builder<DolanItemClass>()
                .setQuery(query, DolanItemClass.class)
                .build();
        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DolanItemClass, DolanImageViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull DolanImageViewHolder dolanImageViewHolder, int i, @NonNull DolanItemClass dolanItemClass) {
                dolanImageViewHolder.showImageArray(dolanItemClass);
            }

            @NonNull
            @Override
            public DolanImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new DolanImageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image_data, parent, false));
            }
        };
        firebaseRecyclerAdapter.notifyDataSetChanged();
        rvImageDetailCollages.setAdapter(firebaseRecyclerAdapter);
    }

    private void LoadDetailData() {
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                DolanItemClass dolanItemClass = dataSnapshot.getValue(DolanItemClass.class);
                tvNamaDetail.setText(dolanItemClass.getName_tourism());
                tvAlamatDetail.setText(dolanItemClass.getLocation_tourism());
                tvDescDetail.setText(dolanItemClass.getInfo_tourism());
                Glide.with(getApplicationContext()).load(dolanItemClass.getUrl_photo()).into(imgDetailData);
                ShowCollage();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        };
        databaseReference.addValueEventListener(eventListener);
        valueEventListener = eventListener;
    }


    @Override
    protected void onStart() {
        super.onStart();
        LoadDetailData();
        if(firebaseRecyclerAdapter!=null){
            firebaseRecyclerAdapter.startListening();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        databaseReference.removeEventListener(valueEventListener);

    }
}

I expected that I can show the array of url_photo_collage node but I don't have any idea for my item class.

The problem in your code is that your url_photo_collage field is declared in your DolanItemClass class of type String while in your database is an array . To solve this, change the type of your field from String to a List<String> and get it accordingly in your adapter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM