我想使用recyclerview在我的应用程序屏幕上显示firebase数据。我的应用程序正在运行,没有任何错误,但数据未显示在屏幕上。 请帮忙 这是我的代码: 请检查并帮助我使用recyclerview
显示从firebase到应用程序屏幕的数据VenuesActivity.java
Grouping everything and then filtering
venuesactivity.xml
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.LayoutInflater;
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.firebase.ui.database.FirebaseRecyclerOptions;
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));
}
FirebaseRecyclerOptions<Blog> options =
new FirebaseRecyclerOptions.Builder<Blog>()
.setQuery(mDatabase, Blog.class)
.build();
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(options) {
@Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.venue_row, parent, false);
return new BlogViewHolder(view);
}
@Override
protected void onBindViewHolder(BlogViewHolder holder, int position, Blog model) {
// Bind the image_details object to the BlogViewHolder
// ...
holder.setTitle(model.getTitle());
holder.setDesc(model.getDesc());
holder.setImage(getApplicationContext(),model.getImage());
}
};
// FirebaseRecyclerAdapter<Blog,BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>
// (Blog.class,R.layout.venue_row,BlogViewHolder.class,mDatabase) {
// @Override
// protected void onBindViewHolder(@NonNull BlogViewHolder viewHolder, int position, @NonNull Blog model) {
//
// viewHolder.setTitle(model.getTitle());
// viewHolder.setDesc(model.getDesc());
// viewHolder.setImage(getApplicationContext(),model.getImage());
//
// }
//
// @NonNull
// @Override
// public BlogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// return null;
// }
// };
mBlogList.setAdapter(adapter);
}
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);
}
}
}
Blog.java
<?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>
venue_row.xml
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(){
}
}
build.gradel
<?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>
答案 0 :(得分:0)
解决这个问题,请按照以下步骤进行:
删除:mBlogList.setHasFixedSize(true);
在adapter
方法中移动onCreate()
的声明。
制作adapter
可变全球:
private FirebaseRecyclerAdapter<Blog, BlogViewHolder> adapter;
从FirebaseRecyclerAdapter<Blog, BlogViewHolder>
方法移除onCreate()
。
在onStart()
和onStop()
方法中添加以下代码行。
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if(adapter != null) {
adapter.stopListening();
}
}
答案 1 :(得分:0)
我认为你必须编写错误的代码。你可以尝试这种方式。
public class SavedRestaurantListActivity extends AppCompatActivity {
private DatabaseReference mRestaurantReference;
private FirebaseRecyclerAdapter mFirebaseAdapter;
@Bind(R.id.recyclerView) RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurants);
ButterKnife.bind(this);
mRestaurantReference = FirebaseDatabase.getInstance().getReference(Constants.FIREBASE_CHILD_RESTAURANTS);
setUpFirebaseAdapter();
}
private void setUpFirebaseAdapter() {
mFirebaseAdapter = new FirebaseRecyclerAdapter<Restaurant, FirebaseRestaurantViewHolder>
(Restaurant.class, R.layout.restaurant_list_item, FirebaseRestaurantViewHolder.class,
mRestaurantReference) {
@Override
protected void populateViewHolder(FirebaseRestaurantViewHolder viewHolder,
Restaurant model, int position) {
viewHolder.bindRestaurant(model);
}
};
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mFirebaseAdapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
mFirebaseAdapter.cleanup();
}
@Override
protected void onStart() {
super.onStart();
mFirebaseAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if(mFirebaseAdapter != null) {
mFirebaseAdapter.stopListening();
}
}
}
答案 2 :(得分:0)
解决方案很简单。请将回收站视图的layout_height从match_parent更改为wrap_content
稍后谢谢
答案 3 :(得分:0)
在设置apdapter之后添加以下行:
mBlogList.setAdapter(adapter);
mFirebaseAdapter.startListening();