使用FirebaseRecyclerOptions检索数据

时间:2019-02-10 13:30:06

标签: android firebase firebase-realtime-database recycler-adapter firebaseui

在使用Firebase recycleview适配器检索数据时,我需要帮助。我尝试了很多方法,但是无法检索数据。

我的主类片段就这样了。

public class AllUsers extends AppCompatActivity {   
RecyclerView recyclerView;
Query databaseReference;
FirebaseRecyclerOptions<Users> options;
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all_users);
    recyclerView = (RecyclerView) findViewById(R.id.allusers_list);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    databaseReference = FirebaseDatabase.getInstance().getReference("Users");

}




 @Override
protected void onStart() {
    Log.d("started","YES");
    super.onStart();
    options = new FirebaseRecyclerOptions.Builder<Users>().setQuery(databaseReference,Users.class).build();
    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {

        @Override
        protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
            Log.d("name : ", model.getName());

            holder.setname(model.getName());

        }

        @NonNull
        @Override
        public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext())
                    .inflate(R.layout.activity_all_users, viewGroup, false);

            return new UsersViewHolder(view);
        }
    };
    firebaseRecyclerAdapter.startListening();


    recyclerView.setAdapter(firebaseRecyclerAdapter);

}
 }
}

UserViewHolderClass:

public class UsersViewHolder extends RecyclerView.ViewHolder {

View view;

public UsersViewHolder(@NonNull View itemView) {
    super(itemView);
    view = itemView;
}

public  void setname (String name) {
    TextView n = view.findViewById(R.id.singleusername);
    n.setText(name);

}
}

用户类别:

public class Users {
public String name;
public String status;
public String image;
public String thumb_image;


public Users() {}

public Users(String name, String status, String image, String thumb_image) {
    this.name = name;
    this.status = status;
    this.image = image;
    this.thumb_image = thumb_image;
}

public String getName() {
    return name;
}

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

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getThumb_image() {
    return thumb_image;
}

public void setThumb_image(String thumb_image) {
    this.thumb_image = thumb_image;
}
}

当我运行该应用程序时,它显示的是空白屏幕而不是数据,因此请告诉我错误的出处。 这是用于firebaseUI 3.0+版本。

指示我收到此错误:

当我将startlistining添加到firebaseRecyclerAdapter时,它显示以下错误。

2019-02-11 19:40:03.027 9059-9059/com.example.bhaskar.chat D/AndroidRuntime: Shutting down VM
2019-02-11 19:40:03.027 9059-9059/com.example.bhaskar.chat E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bhaskar.chat, PID: 9059
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bhaskar.chat/com.example.bhaskar.chat.AllUsers}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.firebase.ui.database.FirebaseRecyclerAdapter.startListening()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.firebase.ui.database.FirebaseRecyclerAdapter.startListening()' on a null object reference
        at com.example.bhaskar.chat.AllUsers.onStart(AllUsers.java:51)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
        at android.app.Activity.performStart(Activity.java:6679)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6077) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
2019-02-11 19:40:03.103 9059-9186/com.example.bhaskar.chat D/NetworkSecurityConfig: No Network Security Config specified, using platform default

1 个答案:

答案 0 :(得分:0)

也许您不调用startListening()开始监听数据

firebaseRecyclerAdapter.startListening();