簡體   English   中英

帶有RecycleView的FirebaseUI

[英]FirebaseUI with RecycleView

在更新FirebaseUI的語法之后,如果沒有onPopulateViewHolder方法將無法正常工作。 我閱讀了FirebaseUI的文檔,並做了同樣的事情。 運行應用程序后, RecycleView會正確運行,但它為空。 我的Firebase的數據沒有出現。 我讀到常見的錯誤是RecycleView使用了參數wrap contentsetHasFixedSize(true) 我以相同的方式進行了所有操作,但是RecycleView仍然為空。 怎么了?

1.等級

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

<?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中的每一行。

     <?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" /> 

  2. Recycle_activity.java,帶有onCreateRecycleViewAdapterViewHolder

     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(); } } 

    }

  3. getterssetters Places.java:

     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數據庫: 屏幕數據庫

ps檢查.write .read的所有規則。

更新:

    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 DatabaseDatabase RulesFirebase StorageStorage 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

要解決此問題,請按照以下步驟操作:

  1. 將模型更改為如下所示:

     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

  2. 讓你的firebaseRecyclerAdapter varaible全球:

     private FirebaseRecyclerAdapter<Places, PlaceViewHolder> firebaseRecyclerAdapter; 
  3. onCreate()方法中刪除FirebaseRecyclerAdapter<Places, PlaceViewHolder>

  4. onStart()onStop()方法中添加以下代碼行。

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

是有關如何從Firebase Realtime數據庫檢索數據並使用FirebaseRecyclerAdapterRecyclerView顯示數據的完整示例。

編輯:

要僅在logcat中顯示這些名稱,請使用以下代碼:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM