【问题标题】:ViewHolder not showing dataViewHolder 不显示数据
【发布时间】:2017-06-22 10:26:31
【问题描述】:

我想根据帖子的时间戳对帖子进行排序。帖子包含标题和描述。我正在使用 firebase 检索数据。但是视图中没有显示任何内容。视图为空白。有什么我做错了吗?

public class AlertsActivity extends AppCompatActivity {
    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.blog_title);
            post_title.setText(title);

        }

        public void setDesp(String desp) {
            TextView post_desp = (TextView) mview.findViewById(R.id.blog_desp);
            post_desp.setText(desp);
        }


        public void setTimestampCreated(HashMap<String, Object> timestamp) {
            TextView show_ts = (TextView) mview.findViewById(blog_timestamp);
            show_ts.setText(String.valueOf(timestamp));
        }
    }

适配器:

      mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
      mDatabase.keepSynced(true);
      mDatabase.orderByChild("timestamp");        

      FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new
            FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
                    Blog.class,
                    R.layout.blog_row,
                    BlogViewHolder.class, mDatabase) {
                @Override
                protected void populateViewHolder(final BlogViewHolder viewHolder, final Blog model, int position) {
                    mDatabase.child("Blog").orderByChild("timestamp").addValueEventListener(new ValueEventListener() {

                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            String alerts = dataSnapshot.getValue(String.class);
                            HashMap<String, Object> time = (HashMap<String, Object>) dataSnapshot.getValue();
                            viewHolder.setTitle(alerts);
                            viewHolder.setDesp(alerts);
                            viewHolder.setTimestampCreated(time);
                        }


                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
                }
            };
    mbloglist.setAdapter(firebaseRecyclerAdapter);
    }

数据库图片: https://i.stack.imgur.com/GNouK.png

【问题讨论】:

  • 有什么我做错了吗? 问题是:有什么你做对了吗 ...你在里面创建适配器从未使用过的异步任务的onProgressUpdate,您的代码未格式化,并且cleary不完整
  • @Selvin 我正在使用异步任务,以便显示进度条。是的,我只展示了需要的重要部分。这不是完整的代码
  • 我只展示了需要的重要部分并且没有意义
  • @Selvin 现在有意义吗?
  • @MadhuBanerjee:你在populateViewHolder 中附加了一个监听器。虽然这可能是必要的,但它并不常见,并且会导致许多潜在的边缘情况。你能显示你试图显示的 JSON 的 sn-p 吗?您可以通过单击 Firebase Database console 中的“导出 JSON”链接来获取此信息。

标签: android firebase firebase-realtime-database


【解决方案1】:

对于这样一个简单的结构,您的代码看起来太复杂了。使用 FirebaseUI 中的适配器的主要目的是让您不必自己处理侦听器。据我所知,您只需要:

mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
Query orderedBlogs = mDatabase.orderByChild("timestamp");        

FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new
        FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
                Blog.class,
                R.layout.blog_row,
                BlogViewHolder.class, orderedBlogs) {
    @Override
    protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
        viewHolder.setTitle(blog.title);
        viewHolder.setDesp(blog.desp);
        viewHolder.setTimestampCreated(blog.timestamp);
    }
};
mbloglist.setAdapter(firebaseRecyclerAdapter);

这是对应的最小Blog类:

public class Blog {
  public String desp;
  public long timestamp;
  public string title;
}

【讨论】:

  • 它还是一样的!经过几天的搜索和尝试所有我不得不使用的组合:LinearLayoutManager mLayoutManager=new LinearLayoutManager(AlertsActivity.this); mLayoutManager.setReverseLayout(true); mLayoutManager.setStackFromEnd(true);由于某种原因,它无法使用 orderbychild()!
  • 该代码只是颠倒了布局。项目是否出现应该没有区别,这就是你最初的帖子所说的不会发生。
  • 哦!只是忘了提及在我尝试了您的代码后这些项目正在显示。我说的是即使在使用 orderbychild() 之后数据也没有排序。所以我不得不使用反向布局的东西。我的错! @Frank van Puffelen
猜你喜欢
  • 2015-11-19
  • 2019-05-25
  • 1970-01-01
  • 2020-05-16
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
相关资源
最近更新 更多