如何从Firebase Realtime Database Java中的子节点检索数组数据? (Firebase回收器适配器) [英] How to retrieve array data from children node in Firebase Realtime Database Java? (Firebase Recycler Adapter)

查看:42
本文介绍了如何从Firebase Realtime Database Java中的子节点检索数组数据? (Firebase回收器适配器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的问题与其他提问者相似,但是并没有解决我的问题,因为我仍然对如何声明作为父节点的孩子的项感到好奇.这是我在Firebase上的结构数据:

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

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

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);
    }
}

还有显示列表的活动类

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);

    }
}

我希望我可以显示url_photo_collage节点的数组,但是我对我的商品类一无所知.

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

推荐答案

您的代码中的问题是,您的url_photo_collage字段在类型为StringDolanItemClass类中声明了,而在数据库中是.要解决此问题,请将字段的类型从String更改为List<String>,并相应地在适配器中获取它.

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.

这篇关于如何从Firebase Realtime Database Java中的子节点检索数组数据? (Firebase回收器适配器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆