0

I have 2 applications(different package names) which use one Firebase database. One app has to write access to the database and another have read access to the database.in my second application, i use recyclerview to retrieve data which is stored by 1st App.

for this I use below code:

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

}

Adapter class

  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

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:

there is no error but my recycler not showing anything

Mrunal
  • 578
  • 2
  • 21
  • 39

2 Answers2

1

go to onStart() and start listening

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

and in your onStop

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

The FirebaseRecyclerAdapter uses a snapshot listener to monitor changes to the Firestore query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.

Be sure that the names of constant in the POJO match exatly the names of your database structure in your firebase console !!

ps: do not post your api-keys or app-ids in your questions, keep them secret, and consider using firebaserecycleradapter if you are using firebase-database , it will be more easy to setup and to show values.

Your POJO is ok !

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
0

Found Solution!! just change this part of a code

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

TO

   FirebaseApp.initializeApp(this);
      // Retrieve my other app.
   FirebaseApp app = FirebaseApp.getInstance("[DEFAULT]");
Mrunal
  • 578
  • 2
  • 21
  • 39