使用RecyclerView显示来自Firebase的数据时出错

时间:2018-05-21 12:05:09

标签: android firebase firebase-realtime-database firebaseui

            (Blog.class,R.layout.venue_row,BlogViewHolder.class,mDatabase) {

onStart()中的这一行显示错误。 在这里,我发布了错误消息的屏幕截图。的 Please check this once.

错误来自Activity.java文件onStart(){...}                 (Blog.class,R.layout.venue_row,BlogViewHolder.class,mDatabase){

Activity.java

package com.cornicore.icccricketworldcup2019;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class VenuesActivity extends AppCompatActivity {

    private RecyclerView mBlogList;
    private DatabaseReference mDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_venues);


        mDatabase = FirebaseDatabase.getInstance().getReference().child("Venues");
        mDatabase.keepSynced(true);
        mBlogList = (RecyclerView) findViewById(R.id.myrecyclerview);
        mBlogList.setHasFixedSize(true);
        mBlogList.setLayoutManager(new LinearLayoutManager(this));



    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Blog,BlogViewHolder,>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>
                (Blog.class,R.layout.venue_row,BlogViewHolder.class,mDatabase) {
            @Override
            protected void onBindViewHolder(@NonNull BlogViewHolder holder, int position, @NonNull Blog model) {

            }

            @NonNull
            @Override
            public BlogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return null;
            }
        };

        mBlogList.setAdapter(firebaseRecyclerAdapter);

    }

    public static class BlogViewHolder extends RecyclerView.ViewHolder{
        View mView;
        public BlogViewHolder(View itemView){
            super(itemView);
            mView = itemView;
        }
        public void setTitle(String title){
            TextView post_title = (TextView) mView.findViewById(R.id.post_title);
            post_title.setText(title);
        }
        public void setDesc(String desc){
            TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
            post_desc.setText(desc);
        }
        public void setImage(Context ctx, String image){
            ImageView post_Image = (ImageView) mView.findViewById(R.id.post_image);
            Picasso.with(ctx).load(image).into(post_Image);
        }
    }

}

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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="match_parent"
    tools:context=".VenuesActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/myrecyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

</RelativeLayout>

Blog.java

package com.cornicore.icccricketworldcup2019;

public class Blog {

    private String title;
    private String desc;
    private String image;

    public Blog(String title, String desc, String image) {
        this.title = title;
        this.desc = desc;
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getImage() {
        return image;
    }

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

    }

}

venue_row.xml(将recyclerview显示为cardview)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_margin="7dp"
    app:cardCornerRadius="12dp"
    android:elevation="90dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="248dp"
        android:orientation="vertical"
        android:background="#121111">

        <ImageView
            android:id="@+id/post_image"
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:scaleType="centerCrop"/>

        <TextView
            android:id="@+id/post_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="POST TITLE"
            android:textColor="#fff"/>

        <TextView
            android:id="@+id/post_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="POST DESC"
            android:textColor="#fff"/>


    </LinearLayout>



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

4 个答案:

答案 0 :(得分:1)

查看此处的文档: FirebaseUI-Readme

他们改变了FirebaseRecyclerAdapter的设置方式。现在,您需要使用FirebaseRecyclerOptions配置FirebaseRecyclerAdapter

答案 1 :(得分:0)

您收到该错误是因为您向构造函数传递了错误的数字和错误类型的参数。为了使其工作,您需要将FirebaseRecyclerOptions对象作为参数传递。在早期版本的Firebase-UI库中需要具有4个参数的构造函数。见official documentation

如果您有兴趣, this 是如何从Firebase实时数据库检索数据并使用RecyclerViewFirebaseRecyclerAdapter中显示数据的。

答案 2 :(得分:0)

使用onStart方法

从代码中删除此逗号
 FirebaseRecyclerAdapter<Blog,BlogViewHolder,>firebaseRecyclerAdapter
                                            ^

答案 3 :(得分:0)

制作适配器,如下面的代码..

public class RecyclerViewAdpater extends RecyclerView.Adapter<RecyclerViewAdpater.ItemViewHolder> {
List<String> mStringList = new ArrayList<>();// hear you can pass any pojo class object.
Context mContext;

public RecyclerViewAdpater(List<String> mStringList, Context mContext) {
    this.mStringList = mStringList;
    this.mContext = mContext;
}

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);
    return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
    // below code handle click event on recycler view item.
    String data=mStringList.get(position);
    holder.textView.setText(data);
}

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

public class ItemViewHolder extends RecyclerView.ViewHolder {
    TextView textView;

    public ItemViewHolder(View itemView) {
        super(itemView);
    }
}
}

然后在主要活动之后获取您的数据,然后在下面的代码之后调用..

        RecyclerViewAdpater recyclerViewAdpater=new RecyclerViewAdpater(this,stringlist);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(recyclerViewAdpater);
    recyclerViewAdpater.notifyDataSetChanged();

确保传递您的数据列表不为空。