问题描述
有人可以告诉该特定行中什么错误.
Can someone please tell what's the error in that specific line.
这是显示的错误消息.
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);
}
}
}
推荐答案
FirebaseUI的最新版本(3.x)与以前的版本相比,实现了一种初始化FirebaseRecyclerAdapter
的不同方法.从使用FirebaseRecyclerAdapter文档:
The latest version (3.x) of FirebaseUI implements a different method of initializing a FirebaseRecyclerAdapter
than previous versions. From the using the FirebaseRecyclerAdapter documentation:
FirebaseRecyclerOptions<Chat> options =
new FirebaseRecyclerOptions.Builder<Chat>()
.setQuery(query, Chat.class)
.build();
接下来创建FirebaseRecyclerAdapter
对象.你应该已经 有一个ViewHolder
子类来显示每个项目.
Next create the FirebaseRecyclerAdapter
object. You should already have a ViewHolder
subclass for displaying each item.
因此,对于您的示例,您需要执行以下操作:
So, for your example, you'll need to do something similar to:
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生命周期文档.
Finally, the FirebaseRecyclerAdapter
uses an event listener to monitor changes to the Firebase query. To begin listening for data, call adapter.startListening()
and adapter.stopListening()
to stop the listener. It's recommended to call these in onStart()
and onStop()
respectively. Further details on this are available in the FirebaseRecyclerAdapter lifecycle documentation.
这篇关于firebaserecycleradapter()无法应用于FirebaseRecyclerAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!