从Firebase数据库检索数据并显示在RecyclerView上

时间:2017-03-29 16:04:27

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

当我运行它时应用程序崩溃,但我的代码没有看到任何错误。我正在尝试将firebase上的数据检索到public class DialogComments extends AppCompatActivity implements View.OnClickListener { private FirebaseAuth mAuithComment; private ProgressDialog mProgressDialog; private EditText etComment; private DatabaseReference dbRef; private FirebaseDatabase mDatabaseComment; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_dialog_comments); recyclerView = (RecyclerView) findViewById(R.id.comment_recyclerview); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); mDatabaseComment = FirebaseDatabase.getInstance(); dbRef = mDatabaseComment.getReference("UserComments"); } @Override protected void onStart(){ super.onStart(); FirebaseRecyclerAdapter<CommentModels, CommentViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<CommentModels, CommentViewHolder> ( CommentModels.class, R.layout.row_comment, CommentViewHolder.class, dbRef ) { @Override protected void populateViewHolder(CommentViewHolder viewHolder, CommentModels model, int position) { viewHolder.setusername(model.getusername()); viewHolder.setcomment(model.getcomment()); } }; recyclerView.setAdapter(firebaseRecyclerAdapter); } public static class CommentViewHolder extends RecyclerView.ViewHolder{ TextView usernameTextView; TextView commentTextView; View mView; public CommentViewHolder(View itemView){ super(itemView); mView = itemView; usernameTextView = (TextView) mView.findViewById(R.id.tv_username); commentTextView = (TextView) mView.findViewById(R.id.tv_comment); } public void setusername(String username){ usernameTextView.setText(username); } public void setcomment(String comment){ commentTextView.setText(comment); } } public void init(){ etComment = (EditText) findViewById(R.id.et_comment); findViewById(R.id.iv_send).setOnClickListener(this); } public void sendcomment(){ } @Override public void onClick(View view) { switch (view.getId()) { case R.id.iv_send: sendcomment(); } } } 。我正在发表评论应用。请帮我看看我的代码有什么问题。

Java代码

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Username"
            android:textColor="@android:color/black"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_comment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:text="dfsd; lfjsd;fk js;dkfj a;sdfj ;sadfj ;sadlkfj ;asdlkfj a;ldskfj ;alkdjfs ;alksdjf ;alkjdsf"
            android:textColor="@android:color/black" />

    </LinearLayout>

   <!-- <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="10h30" /
</LinearLayout>
</android.support.v7.widget.CardView>

row_comment.xml

package com.feu.teamanonymous.ayokotseph_final.Variables;
import java.io.Serializable;
public class CommentModels implements Serializable {
    String username;
    String comment;
    public CommentModels(String userName, String comment) {
        this.username = userName;
        this.comment = comment;
    }

    public String getusername() {
        return username;
    }

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

    public String getcomment() {
        return comment;
    }

    public void setuComment(String comment) {
        this.comment = comment;
    }
}

CommentModels.java

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="450dp"
    tools:context="com.feu.teamanonymous.ayokotseph_final.Activities.DialogComments"
    android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/comment_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

        </android.support.v7.widget.RecyclerView>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <EditText
                android:id="@+id/et_comment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="Type comment..." />

            <ImageView
                android:id="@+id/iv_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginBottom="4dp"
                android:layout_marginLeft="8dp"
                android:src="@drawable/ic_send_black_24dp" />
        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

acitivity_dialog_comment.xml

queryNames

0 个答案:

没有答案