带有RecycleView的FirebaseUI [英] FirebaseUI with RecycleView

查看:46
本文介绍了带有RecycleView的FirebaseUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新FirebaseUI的语法后,如果没有onPopulateViewHolder方法,将无法使用.我阅读了FirebaseUI的文档,并做了同样的事情.运行应用程序后,RecycleView正常运行,但为空.我的Firebase的数据没有出现.我读到常见错误是RecycleView使用参数wrap contentsetHasFixedSize(true).我以相同的方式进行了所有操作,但是RecycleView仍然为空.怎么了?

After uptade syntax of FirebaseUI, can't work without onPopulateViewHolder method. I read the doc of FirebaseUI and did the same. After running app, RecycleView is running without mistake, but it empty. The data of my Firebase didn't appear. I read that the common mistake is that RecycleView used the parameter wrap content or setHasFixedSize(true). I did everything the same way, but the RecycleView is still empty. What is wrong?

1.Gradle:

apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.eugene.lafinalproduction"
    minSdkVersion 15
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.google.firebase:firebase-database:15.0.0'
implementation 'com.google.firebase:firebase-storage:15.0.0'
implementation 'com.google.firebase:firebase-auth:15.0.0'
implementation 'com.firebaseui:firebase-ui-database:3.2.2'
implementation 'com.android.support:recyclerview-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso- 
core:3.0.1'
//Glide library
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}
apply plugin: 'com.google.gms.google-services'

2.Recycle_activity.xml

2.Recycle_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=".Recycle_activity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/list_result"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:scrollbars="vertical"/>
</RelativeLayout>

  1. Item_list.xml-我在RV中的每一行.

  1. Item_list.xml - my each row in RV.

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/image_id"
    android:layout_width="330dp"
    android:layout_height="200dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    app:srcCompat="@drawable/img_2" />

<TextView
    android:id="@+id/text_image_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="24dp"
    android:textColor="@color/myTextColor"
    android:textSize="85sp" />

Recycle_activity.java,带有onCreateRecycleViewAdapterViewHolder

Recycle_activity.java, with onCreate, RecycleViewAdapter, ViewHolder

public class Recycle_activity extends AppCompatActivity {

private RecyclerView mResultList;
private DatabaseReference mPlaceDatabase;
private Query query;
private FirebaseRecyclerAdapter<Places, PlaceViewHolder> firebaseRecyclerAdapter;
private DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
private DatabaseReference usersRef = rootRef.child("Users");


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recycle_activity);

    mResultList = findViewById(R.id.list_result);

    mPlaceDatabase = FirebaseDatabase.getInstance().getReference();
    query = mPlaceDatabase.child("Users");

    //mResultList.setHasFixedSize(true);
    mResultList.setLayoutManager(new LinearLayoutManager(this));
    //mLinearLayouManager = new LinearLayoutManager(this);
    FirebaseRecyclerOptions<Places> options =
            new FirebaseRecyclerOptions.Builder<Places>()
                    .setQuery(query, Places.class)
                    .build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Places, PlaceViewHolder>(options) {
        @Override
        protected void onBindViewHolder(PlaceViewHolder holder, int position, Places model) {

            holder.setDetails(getApplicationContext(), model.getName(), model.getImage());
            Toast.makeText(getApplicationContext(), model.getName(), Toast.LENGTH_SHORT).show();

        }

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

    };
    mResultList.setAdapter(firebaseRecyclerAdapter);
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class);
                Log.d("TAG", name);
                Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };
    usersRef.addListenerForSingleValueEvent(valueEventListener);
}
@Override
protected void onStart() {
    super.onStart();
    firebaseRecyclerAdapter.startListening();
}

class PlaceViewHolder extends RecyclerView.ViewHolder {

    View mView;

    public PlaceViewHolder(View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setDetails(Context context, String placeName, String placeImage) {

        TextView place_Name = mView.findViewById(R.id.image_id);
        ImageView place_Image = mView.findViewById(R.id.text_image_id);

        place_Name.setText(placeName);
        Glide.with(context).load(placeImage).into(place_Image);
    }
}

@Override
protected void onStop() {
    super.onStop();
    if (firebaseRecyclerAdapter != null) {
        firebaseRecyclerAdapter.stopListening();
    }
}

}

带有getterssetters的Places.java:

Places.java with getters,setters:

public class Places {

public String name_place, image_place;

public Places() {

}

public String getName_place() {
    return name_place;
}

public void setName_place(String name_place) {
    this.name_place = name_place;
}

public String getImage_place() {
    return image_place;
}

public void setImage_place(String image_place) {
    this.image_place = image_place;
}

public Places(String name_place, String image_place) {
    this.name_place = name_place;
    this.image_place = image_place;
}
}

还有我的Firebase数据库:屏幕数据库

And my Firebase Database: Screen Database

p.s.检查所有.write .read规则.

p.s. All rules of .write .read are checked.

更新:

    public class Recycle_activity extends AppCompatActivity {

    private RecyclerView mResultList;
    private DatabaseReference mPlaceDatabase;
    private Query query;
    private FirebaseRecyclerAdapter<Places, PlaceViewHolder> firebaseRecyclerAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_activity);

        mResultList = findViewById(R.id.list_result);

        mPlaceDatabase = FirebaseDatabase.getInstance().getReference();
        query = mPlaceDatabase.child("Users");

        //mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));
        //mLinearLayouManager = new LinearLayoutManager(this);
        FirebaseRecyclerOptions<Places> options =
                new FirebaseRecyclerOptions.Builder<Places>()
                        .setQuery(query, Places.class)
                        .build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Places, PlaceViewHolder>(options) {
            @Override
            protected void onBindViewHolder(PlaceViewHolder holder, int position, Places model) {

                holder.setDetails(getApplicationContext(), model.getName(), model.getImage());

            }

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

        };
        mResultList.setAdapter(firebaseRecyclerAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }

    class PlaceViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public PlaceViewHolder(View itemView) {
            super(itemView);

            mView = itemView;
        }

        public void setDetails(Context context, String placeName, String placeImage) {

            TextView place_Name = mView.findViewById(R.id.image_id);
            ImageView place_Image = mView.findViewById(R.id.text_image_id);

            place_Name.setText(placeName);
            Glide.with(context).load(placeImage).into(place_Image);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }
}

更新的数据库和存储: Firebase Database Database Rules Firebase Storage Storage Rules .

Updated database and storage: Firebase Database, Database Rules, Firebase Storage, Storage Rules.

启动后的日志:

    04-24 01:17:41.304 7093-7093/? I/art: Late-enabling -Xcheck:jni
04-24 01:17:41.345 7093-7093/com.example.eugene.lafinalproduction W/ResourceType: Found multiple library tables, ignoring...
04-24 01:17:41.353 7093-7093/com.example.eugene.lafinalproduction W/System: ClassLoader referenced unknown path: /data/app/com.example.eugene.lafinalproduction-2/lib/arm64
04-24 01:17:41.396 7093-7093/com.example.eugene.lafinalproduction W/ComponentDiscovery: Application info not found.
    Could not retrieve metadata, returning empty list of registrars.
04-24 01:17:41.439 7093-7093/com.example.eugene.lafinalproduction I/FirebaseInitProvider: FirebaseApp initialization successful
04-24 01:17:41.477 7093-7093/com.example.eugene.lafinalproduction W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-24 01:17:41.524 7093-7110/com.example.eugene.lafinalproduction I/FA: App measurement is starting up, version: 12451
    To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
    To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.example.eugene.lafinalproduction
04-24 01:17:41.589 7093-7112/com.example.eugene.lafinalproduction I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
    Selected remote version of com.google.android.gms.firebase_database, version >= 6
04-24 01:17:41.606 7093-7112/com.example.eugene.lafinalproduction W/ResourceType: Found multiple library tables, ignoring...
04-24 01:17:41.607 7093-7110/com.example.eugene.lafinalproduction I/FA: Tag Manager is not found and thus will not be used
04-24 01:17:41.613 7093-7093/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7f9d259920), client(37), share_fd(34)
04-24 01:17:41.627 7093-7112/com.example.eugene.lafinalproduction W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000018/n/arm64-v8a
04-24 01:17:41.642 7093-7112/com.example.eugene.lafinalproduction W/ResourceType: Found multiple library tables, ignoring...
04-24 01:17:41.685 7093-7102/com.example.eugene.lafinalproduction I/System: FinalizerDaemon: finalize objects = 1
04-24 01:17:41.689 7093-7113/com.example.eugene.lafinalproduction E/GED: Failed to get GED Log Buf, err(0)
04-24 01:17:41.689 7093-7113/com.example.eugene.lafinalproduction I/OpenGLRenderer: Initialized EGL, version 1.4
04-24 01:17:41.699 7093-7113/com.example.eugene.lafinalproduction I/OpenGLRenderer: Get enable program binary service property (1)
    Initializing program atlas...
04-24 01:17:41.700 7093-7113/com.example.eugene.lafinalproduction I/OpenGLRenderer: Program binary detail: Binary length is 146348, program map length is 128.
    Succeeded to mmap program binaries. File descriptor is 42, and path is /dev/ashmem�.
    No need to use file discriptor anymore, close fd(42).
04-24 01:17:41.709 7093-7113/com.example.eugene.lafinalproduction W/libEGL: [ANDROID_RECORDABLE] format: 1
04-24 01:17:41.710 7093-7113/com.example.eugene.lafinalproduction I/PerfService: PerfServiceNative api init
04-24 01:17:41.716 7093-7113/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7fa7631fa0), client(37), share_fd(44)
04-24 01:17:41.738 7093-7113/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7fa7631aa0), client(37), share_fd(47)
04-24 01:17:41.741 7093-7131/com.example.eugene.lafinalproduction I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
04-24 01:17:41.741 7093-7131/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: netid=0; mark=0
04-24 01:17:41.742 7093-7131/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
    [getaddrinfo]: netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
04-24 01:17:41.743 7093-7093/com.example.eugene.lafinalproduction I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@5d4e48d time:222822867
04-24 01:17:41.746 7093-7131/com.example.eugene.lafinalproduction D/libc-netbsd: getaddrinfo: get result from proxy gai_error = 7
04-24 01:17:41.746 7093-7131/com.example.eugene.lafinalproduction I/System.out: [CDS][DNS]Unable to resolve host "lafinalproduction.firebaseio.com": No address associated with hostname
04-24 01:17:42.574 7093-7135/com.example.eugene.lafinalproduction I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
04-24 01:17:42.574 7093-7135/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
04-24 01:17:43.242 7093-7113/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7fa7632180), client(37), share_fd(49)
04-24 01:17:43.256 7093-7113/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7fa76322c0), client(37), share_fd(51)
04-24 01:17:43.767 7093-7136/com.example.eugene.lafinalproduction I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
04-24 01:17:43.767 7093-7136/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: netid=0; mark=0
04-24 01:17:43.768 7093-7136/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
    [getaddrinfo]: netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0

推荐答案

要解决此问题,请按照以下步骤操作:

To solve this, please follow the next steps:

  1. 将模型更改为如下所示:

  1. change your model to look like this:

public class Places {
    private String image, name;

    public Places() { }

    public Places(String image, String name) {
        this.image = image;
        this.name = name;
    }

    public String getImage() { return image; }
    public String getName() { return name; }
}

模型类中的字段应与数据库中的字段完全相同.在您的代码中有所不同.参见name_placename.

The fields from your model class should look exactly like the one from your database. In your code are different. See name_place vs. name.

将您的firebaseRecyclerAdapter变量设为全局变量:

Make your firebaseRecyclerAdapter varaible global:

private FirebaseRecyclerAdapter<Places, PlaceViewHolder> firebaseRecyclerAdapter;

  • onCreate()方法中删除FirebaseRecyclerAdapter<Places, PlaceViewHolder>.

  • Remove FirebaseRecyclerAdapter<Places, PlaceViewHolder> from the onCreate() method.

    onStart()onStop()方法中添加以下代码行.

    Add the following lines of code in the onStart() and onStop() methods.

    @Override
    protected void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if(firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }
    

  • 是一个完整的示例,说明如何从Firebase Realtime数据库检索数据并使用FirebaseRecyclerAdapterRecyclerView中显示它们.

    要简单地在logcat中显示这些名称,请使用以下代码:

    To simply display those names in the logcat, please use the following code:

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference usersRef = rootRef.child("Users");
    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class);
                Log.d("TAG", name);
                Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show());
            }
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };
    usersRef.addListenerForSingleValueEvent(valueEventListener);
    

    这篇关于带有RecycleView的FirebaseUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆