无法从firebase单个数据库检索从一个应用程序到另一个应用程序的数据

问题描述 投票:0回答:2

我有2个使用一个Firebase数据库的应用程序(不同的软件包名称)。一个应用程序必须写入数据库的访问权限,另一个应用程序具有对数据库的读访问权。在我的第二个应用程序中,我使用recyclerview来检索由第一个应用程序存储的数据。

为此,我使用下面的代码:

FirebaseOptions options = new FirebaseOptions.Builder()
            .setApplicationId("1:567....259c8f58311") // Required for Analytics.
            .setApiKey("AIzaSyA9BRxl......hE03y5qD-c") // Required for Auth.
            .setDatabaseUrl("https://mycity-3a561.firebaseio.com/") // Required for RTDB.
            .build();
    FirebaseApp.initializeApp(this /* Context */, options, "MyCity");
         // Retrieve my other app.
    FirebaseApp app = FirebaseApp.getInstance("MyCity");
          // Get the database for the other app.
    FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);
    DatabaseReference data = secondaryDatabase.getInstance().getReference();
    data.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot snapshot) {

            for (DataSnapshot ds : snapshot.getChildren()) {

                for (DataSnapshot dSnapshot : ds.getChildren()) {

                    WaterClass waterClass = dSnapshot.getValue(WaterClass.class);

                    Log.d("Show", waterClass.getName() == null ? "" : waterClass.getName());
                    list.add(waterClass);

                }
                adapter = new WaterAdapter(ShowWaterDetails.this, list);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                progressDialog.dismiss();
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });

}

适配器类

  private class WaterAdapter extends RecyclerView.Adapter<WaterAdapter.ViewHolder> {

    ShowWaterDetails showDetail;
    List<WaterClass> listData;

    public WaterAdapter(ShowWaterDetails showWaterDetails, List<WaterClass> list) {

        this.showDetail = showWaterDetails;
        this.listData = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_items, parent, false);
        WaterAdapter.ViewHolder viewHolder = new WaterAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(WaterAdapter.ViewHolder holder, int position) {

        WaterClass AllDetails = listData.get(position);
        holder.NameTextView.setText(AllDetails.getName());
        holder.DetailTextView.setText(AllDetails.getDetail());
        holder.DateTextView.setText(AllDetails.getDate());
        holder.LocationTextView.setText(AllDetails.getLocation());
        holder.TypeTextView.setText(AllDetails.getType());
        Picasso.with(showDetail).load(AllDetails.getImgurl()).resize(120, 60).into(holder.ImageTextView);
    }

    @Override
    public int getItemCount() {

        return listData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public TextView NameTextView;
        public TextView DetailTextView;
        public TextView DateTextView;
        public TextView LocationTextView;
        public TextView TypeTextView;
        public ImageView ImageTextView;

        public ViewHolder(View itemView) {

            super(itemView);
            NameTextView = itemView.findViewById(R.id.ShowNameTextView);
            DetailTextView = itemView.findViewById(R.id.ShowDetailTextView);
            DateTextView = itemView.findViewById(R.id.ShowDateTextView);
            LocationTextView = itemView.findViewById(R.id.ShowLocationTextView);
            TypeTextView = itemView.findViewById(R.id.ShowTypeTextView);
            ImageTextView = itemView.findViewById(R.id.ShowImageView);

        }
    }
}
}

POJO班

class WaterClass {

private String id;
private String email;
private String name;
private String type;
private String detail;
private String location;
private String date;
private String imgurl;

public WaterClass(){
}

public WaterClass(String id, String currentUserString, String imageUrl, String nameString, String typeString, String detailString, String locationString, String dateString) {

    this.id = id;
    this.email = currentUserString;
    this.name =nameString;
    this.type = typeString;
    this.detail = detailString;
    this.location = locationString;
    this.date = dateString;
    this.imgurl = imageUrl;
}

public String getImgurl() {
    return imgurl;
}

public void setImgurl(String imgurl) {
    this.imgurl = imgurl;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getDetail() {
    return detail;
}

public void setDetail(String detail) {
    this.detail = detail;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

}

Database Structure

没有错误,但我的回收商没有显示任何东西

android firebase-realtime-database
2个回答
1
投票

转到onStart()并开始收听

   @Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

并在你的onStop

    @Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}

FirebaseRecyclerAdapter使用快照侦听器来监视对Firestore查询的更改。要开始侦听数据,请调用startListening()方法。您可能想在onStart()方法中调用它。在调用startListening()之前,请确保已完成读取数据所需的任何身份验证,否则您的查询将失败。

确保POJO中常量的名称与firebase控制台中的数据库结构名称完全匹配!

ps:不要在你的问题中发布api-keys或app-id,保密,并考虑使用firebaserecycleradapter如果你使用firebase-database,它将更容易设置和显示值。

你的POJO还可以!


1
投票

找到解决方案只需更改代码的这一部分

  FirebaseApp.initializeApp(this /* Context */, options, "MyCity");
     // Retrieve my other app.
  FirebaseApp app = FirebaseApp.getInstance("MyCity");

   FirebaseApp.initializeApp(this);
      // Retrieve my other app.
   FirebaseApp app = FirebaseApp.getInstance("[DEFAULT]");
© www.soinside.com 2019 - 2024. All rights reserved.