0

I have gone through similar problems faced by other coders, but they at least have data displayed in their recycler view, in mine there is nothing displayed and the recycler view is empty, my firebase database is not empty either. No other error message is displayed that indicates database issue. A code snippet of the changes i need to make to my code would be very helpful as I am new to programming using Android Studios.

Main Activity Code

    package com.example.jamsecure;

import android.os.Bundle;
import android.widget.Toast;

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.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

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

public class User_Select_Jam_Studio extends AppCompatActivity {

    List<FetchData> fetchData;
    RecyclerView recyclerView;
    HelperAdapter helperAdapter;
    DatabaseReference databaseReference;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user__select__jam__studio);
        recyclerView=findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager((new LinearLayoutManager(this)));
        fetchData=new ArrayList<>();

        databaseReference= FirebaseDatabase.getInstance().getReference("Owners");

        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot ds:dataSnapshot.getChildren())
                {
                    FetchData data=ds.getValue(FetchData.class);
                    fetchData.add(data);
                }
                helperAdapter=new HelperAdapter(fetchData);
                recyclerView.setAdapter(helperAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(User_Select_Jam_Studio.this,"Data base problem",Toast.LENGTH_LONG).show();
            }
        });

    }
}

HelperAdapter.java

    package com.example.jamsecure;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

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

public class HelperAdapter extends RecyclerView.Adapter {
    List<FetchData> fetchDataList;

    public HelperAdapter(List<FetchData> fetchDataList) {
        this.fetchDataList = fetchDataList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.jamrooms,parent, false);
        ViewHolderClass viewHolderClass = new ViewHolderClass(view);
        return viewHolderClass;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    ViewHolderClass viewHolderClass=(ViewHolderClass)holder;
        FetchData fetchData=fetchDataList.get(position);
        viewHolderClass.name.setText(fetchData.getName());
        viewHolderClass.jamr.setText(fetchData.getJamrate());
        viewHolderClass.mob.setText(fetchData.getMob());

     }

    @Override
    public int getItemCount() {
        return fetchDataList.size();
    }
    public class ViewHolderClass extends RecyclerView.ViewHolder{
        TextView name,jamr,mob;
        public ViewHolderClass(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.name);
            jamr=itemView.findViewById(R.id.jamrate);
            mob=itemView.findViewById(R.id.mob);
        }
    }
}

FetchData.java

package com.example.jamsecure;

public class FetchData {
    String sname;
    String jam_rate;
    String phone;

    public FetchData( String name, String jamrate, String mob) {
        this.sname = name;
        this.jam_rate = jamrate;
        this.phone = mob;
    }
    public FetchData(){}

    public String getName() {
        return sname;
    }

    public String getJamrate() {
        return jam_rate;
    }

    public String getMob() {
        return phone;
    }

}
  • That message happens when you attempt to display a RecyclerView without first attaching an adapter. Your call to setAdapter doesn't happen until some time after the query finishes. – Doug Stevenson Dec 04 '20 at 19:47
  • @DougStevenson sir could you please help me with the code to attach an empty adapter or a way to resolve this issue, as I am new to programming at android studios, also the recycler doesnt display any of my data that is available in the database, along with the error message. – Sujay Hazra Dec 04 '20 at 22:18

0 Answers0