0
votes

I want to make recyclerview by data from firebase. My firebase database structure looks like below.


enter image description here

However, my code failed to data into recyclerview. and I spent lots of times where the problem is. and it looks like addValueEventListenr issue. I dont know why but this listener doesnt proceed.

Adapter class

public class SymbolAdapter extends RecyclerView.Adapter<SymbolAdapter.SymbolHolder>{

  ArrayList<Symbol> symbol_list;
    public SymbolAdapter(ArrayList<Symbol> symbol_list){
        this.symbol_list = symbol_list;
    }
    @NonNull
    @Override
    public SymbolHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_item, parent, false);
        return new SymbolHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SymbolHolder holder, int position) {
        Symbol symbol = symbol_list.get(position);
        holder.symbol.setText(symbol.getSymbol());
        holder.companyName.setText(symbol.getCompanyName());
    }

    @Override
    public int getItemCount() {
        return symbol_list.size();
    }

    class SymbolHolder extends RecyclerView.ViewHolder {
        TextView symbol, companyName;
        public SymbolHolder(@NonNull View itemView){
            super(itemView);
            symbol = itemView.findViewById(R.id.symbol);
            companyName = itemView.findViewById(R.id.companyName);
        }
    }
}

Symbol Class

public class Symbol {
    private String symbol;
    private String companyName;

    public Symbol(){

    }
    public Symbol(String symbol, String companyName){
        this.symbol = symbol;
        this.companyName = companyName;
    }
    public String getSymbol() {
        return symbol;
    }

    public void setSymbol(String symbol) {
        this.symbol = symbol;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
}

Search Fragment

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         View view = inflater.inflate(R.layout.fragment_search, container, false);

         recyclerView = view.findViewById(R.id.search_item_recylerView);
         recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
         recyclerView.setHasFixedSize(true);
         list = new ArrayList<>();

        ref= FirebaseDatabase.getInstance().getReference();
        ref.addValueEventListener(new ValueEventListener() {
             @Override
             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                 Log.d("symbol", "executing? ");

                 if(dataSnapshot.exists()){
                     for (DataSnapshot ds : dataSnapshot.getChildren()){
                         Symbol s =ds.getValue(Symbol.class);
                         Log.d("symbol", s.getSymbol());
                         Log.d("symbol", s.getCompanyName());
                         list.add(s);
                     }
                     madapter= new SymbolAdapter(list);
                     recyclerView.setAdapter(madapter);
                 }
             }

             @Override
             public void onCancelled(@NonNull DatabaseError databaseError) {

             }
         });
        return view;
    }

When I run, the all logs that i put inside of addValueEventListener doesn't reach. Is it because of Connection with firebase? I set to my database rule as true for read. and I added to androidmanifest. However, it still doesn't work. and I checked connection with

DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
        connectedRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                boolean connected = snapshot.getValue(Boolean.class);
                if (connected) {
                    Log.d("symbol", "connected");
                } else {
                    Log.d("symbol", "not connected");
                }
            }

Then addValueEventListener look reach to onDataChange but "not connected" is printed even if I added to manifest.

Thank you so much

1
chck if onCancelled is called? tyr put logs in onCancelled - Asad Ali

1 Answers

0
votes

If the not connected is printed from your listener on .info/connected that means that your app is not connected to the Firebase Database server. Since that is the case, your other handlers won't be able to get any data from the server, and their onDataChange won't fire.

To solve this problem, you'll need to figure out why the Firebase client can't connect to the server. This is typically something either with the internet connection on your device/emulator, or something along the way between your client and the server blocking the traffic. If the reason isn't obvious from logcat output, consider enabling debug logging in the Firebase client to see its detailed output.