简体   繁体   中英

Unable to view single user data from firebase using RecyclerView

Desired Behaviour: I am trying to display a single user "email" and "username" data from Firebase after login. At present, my app crashes when I log in and logcat shows this error:

Can't convert object of type java.lang.String to type com.example.start.model

Note: I am pushing username from the login page using intent.putExtra()

My database snapshot: database snapshot

Main class:

 package com.example.start;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Toast;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import com.firebase.ui.database.FirebaseRecyclerAdapter;
    import com.firebase.ui.database.FirebaseRecyclerOptions;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    
    public class BeginStart extends AppCompatActivity {
    
        DatabaseReference ref;
        FirebaseDatabase rootNode;
       private RecyclerView recyclerView;
       String Receive_username;
    
        private FirebaseRecyclerOptions<model> options;
        private FirebaseRecyclerAdapter<model, MyViewHolder> adapter;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.testlayout);
    
             Intent intent = getIntent();
    Receive_username = intent.getStringExtra("push_username");

    rootNode = FirebaseDatabase.getInstance();
    ref = rootNode.getReference("Users").child(Receive_username);
    
    
            recyclerView =findViewById(R.id.testrecycle);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
    
options = new FirebaseRecyclerOptions.Builder<model>().setQuery(ref, model.class).build();
     adapter = new FirebaseRecyclerAdapter<model, MyViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull model model) {
                    Toast.makeText(getApplicationContext(), "On Bind!", Toast.LENGTH_SHORT).show();
                    holder.textViewname.setText(model.getUsername());
                    holder.textViewemail.setText(model.getEmail());
                }
    
                @NonNull
                @Override
                public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                   View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_view_layout,parent,false);
                    return new MyViewHolder(v);
                }
            };
    
            adapter.startListening();
            recyclerView.setAdapter(adapter);
    
        }
    }

Model Class:

package com.example.start;

public class model {

    String username, email;

    public model() {
    }

    public model(String username, String email) {
        this.username = username;
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

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

View Handler Class:

package com.example.start;

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

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

class MyViewHolder extends RecyclerView.ViewHolder {

TextView textViewname, textViewemail;




    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        textViewname = itemView.findViewById(R.id.textviewname);
        textViewemail = itemView.findViewById(R.id.textviewemail);
    }
}

recyclerview layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/testrecycle"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

Single view layout for RecyclerView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textviewname"
    android:text="title"
    android:textSize="30dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textviewemail"
        android:text="email"
        android:textSize="30dp"/>





</LinearLayout>

When you are passing the following reference:

ref = rootNode.getReference("Users").child(Receive_username);

And:

model.class

To the setQuery() method, it means that you are trying to read all objects that exist under your Receive_username node as model class objects, which is actually not possible because under that node, there are only String values, hence that error.

I am trying to display a single user "email" and "username" data from firebase after login.

If you only need to read the "email" and "username" of a single user, then there is no need to use Firebase-UI library at all. This library is used, for example, when you need to display all users within Users node in a RecyclerView.

If you only need to display the value of those properties that correspond to a single user, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = rootRef.child("Users").child(Receive_username);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String email = dataSnapshot.child("email").getValue(String.class);
        String username = dataSnapshot.child("username").getValue(String.class);
        Log.d("TAG", email + "/" + username);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
    }
};
userRef.addListenerForSingleValueEvent(valueEventListener);

If your Receive_username variable holds the value of Jim , the result in the logcat will be:

jim@test.com/Jim

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM