You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用RecyclerView无法显示Firebase数据问题求助

Hey there, let's walk through the issues in your code that are causing the RecyclerView not to show data from Firebase. I've spotted a few key problems and their fixes:

1. Missing No-Arg Constructor in the Blog Class

Firebase Realtime Database requires a no-argument constructor to deserialize data into your Blog objects. Right now your class only has a parameterized constructor, which means Firebase can't create instances of it when fetching data. Also, your setImage method appears to be incomplete.

Fix the Blog class like this:

package com.cornicore.icccricketworldcup2019;

public class Blog {
    private String title;
    private String desc;
    private String image;

    // Required empty constructor for Firebase deserialization
    public Blog() {}

    public Blog(String title, String desc, String image) {
        this.title = title;
        this.desc = desc;
        this.image = image;
    }

    // Getters and complete setters
    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; }
}

2. Forgot to Start Listening with the FirebaseRecyclerAdapter

FirebaseUI's FirebaseRecyclerAdapter won't fetch data unless you explicitly start its listener. You created the adapter but never called adapter.startListening() in onStart(). Also, it's best practice to stop listening when the activity stops to avoid memory leaks.

First, add a class-level adapter variable to VenuesActivity, then update the lifecycle methods:

public class VenuesActivity extends AppCompatActivity {
    private RecyclerView mBlogList;
    private DatabaseReference mDatabase;
    private FirebaseRecyclerAdapter<Blog, BlogViewHolder> adapter; // Add this

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_venues);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Venues");
        mDatabase.keepSynced(true);
        mBlogList = 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();
        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) {
                holder.setTitle(model.getTitle());
                holder.setDesc(model.getDesc());
                holder.setImage(getApplicationContext(), model.getImage());
            }
        };
        mBlogList.setAdapter(adapter);
        adapter.startListening(); // Critical: Start fetching data
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (adapter != null) {
            adapter.stopListening(); // Stop listening to avoid memory leaks
        }
    }

    // ... Your existing BlogViewHolder class ...
}

3. Verify Firebase Database Rules and Data Structure

  • Check Rules: Ensure your Firebase Realtime Database allows read access (for testing, you can temporarily use these rules, but secure them later):
    {
      "rules": {
        ".read": true,
        ".write": true
      }
    }
    
  • Match Data Structure: Confirm your Venues node in Firebase has child items with exactly the fields title, desc, and image (all string values, matching your Blog class).

4. Double-Check the venue_row.xml Layout

Make sure your row layout has the correct view IDs (post_title, post_desc, post_image) and that views aren't set to zero height/width or hidden (e.g., visibility="gone").

Once you apply these fixes, your RecyclerView should start displaying data from Firebase. Let me know if you run into any other issues!

内容的提问来源于stack exchange,提问作者Badal Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:29:46