gpt4 book ai didi

java - 如何从 Firebase 实时数据库 Java 中的子节点检索数组数据? (Firebase 回收器适配器)

转载 作者:行者123 更新时间:2023-12-02 09:40:15 24 4
gpt4 key购买 nike

我知道我的问题与其他提问者类似,但它没有解决我的问题,因为我仍然好奇如何声明作为父节点的子节点的项目。这是我的 Firebase 上的结构数据:

enter image description here

我尝试为我的案例创建类:

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

以及显示列表的 Activity 类

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 节点数组,但我对我的项目类没有任何想法。

最佳答案

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

关于java - 如何从 Firebase 实时数据库 Java 中的子节点检索数组数据? (Firebase 回收器适配器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57141791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com