firebaserecycleradapter

firebaserecycleradapter

有人可以说出该特定行中的错误吗?

android - firebaserecycleradapter()无法应用于FirebaseRecyclerAdapter-LMLPHP

这是显示的错误消息。

android - firebaserecycleradapter()无法应用于FirebaseRecyclerAdapter-LMLPHP

package com.example.souravkumar.sqaurewallpapers;

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
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;

/**
 * Created by Sourav Kumar on 11/3/2017.
 */

public class popular extends AppCompatActivity {

    private RecyclerView recyclerView;
    private DatabaseReference myRef;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        myRef = FirebaseDatabase.getInstance().getReference();
        FirebaseRecyclerAdapter<image_details, BlogViewHolder> adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(
                image_details.class,
                R.layout.individual_row,

                myRef) {
            @Override
            protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
                holder.setDate(model.getDate());
                holder.setUrl(model.getUrl());
            }

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

        recyclerView.setAdapter(adapter);

    }

    public static  class BlogViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        ImageView imageView;
        public BlogViewHolder(View itemView) {
            super(itemView);
            textView = (TextView)itemView.findViewById(R.id.date);
            imageView = (ImageView)itemView.findViewById(R.id.imageView);
        }

        public void setDate(Long date) {
            textView.setText(date.toString());
        }

        public void setUrl(String url) {
            Picasso.with(itemView.getContext())
                    .load(url)
                    .resize(50, 50)
                    .centerCrop()
                    .into(imageView);
        }
    }
}


Link to the whole code

最佳答案

FirebaseUI的最新版本(3.x)与以前的版本相比,实现了不同的初始化FirebaseRecyclerAdapter的方法。从using the FirebaseRecyclerAdapter documentation


  首先,通过构建FirebaseRecyclerOptions配置适配器。在
  在这种情况下,我们将继续我们的聊天示例:

 FirebaseRecyclerOptions<Chat> options =
                new FirebaseRecyclerOptions.Builder<Chat>()
                        .setQuery(query, Chat.class)
                        .build();

  
  接下来创建FirebaseRecyclerAdapter对象。你应该已经
  有一个ViewHolder子类来显示每个项目。


因此,对于您的示例,您将需要执行以下操作:

FirebaseRecyclerOptions<image_details> options =
        new FirebaseRecyclerOptions.Builder<image_details>()
                .setQuery(myRef, image_details.class)
                .build();

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(options) {
    @Override
    public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.individual_row, parent, false);

        return new BlogViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
        // Bind the image_details object to the BlogViewHolder
        // ...
    }
};


最后,FirebaseRecyclerAdapter使用事件侦听器监视Firebase查询的更改。要开始监听数据,请调用adapter.startListening()adapter.stopListening()停止监听器。建议分别在onStart()onStop()中调用它们。有关更多详细信息,请参见FirebaseRecyclerAdapter lifecycle documentation

07-25 21:28