如何从Firebase检索数据到我的适配器

时间:2018-03-20 12:01:28

标签: android firebase firebase-realtime-database

我的结构

enter image description here

所以我有一个应用程序,用户在我的适配器上传帖子。我可以检索帖子描述和帖子图片,但是当我尝试检索海报的名称时,应用程序似乎崩溃了,为了检索每个帖子的名称,有一个孩子使用上传者添加到数据库&#39 ; s id。这是我的班级文件:

public String image_thumb;
public String user_id;
public String image_url;
public String desc;

public BlogPost(){}


public BlogPost(String user_id, String image_url, String desc, String image_thumb) {
    this.user_id = user_id;
    this.image_url = image_url;
    this.desc = desc;
    this.image_thumb = image_thumb;
}



public String getUser_id() {
    return user_id;
}

public void setUser_id(String user_id) {
    this.user_id = user_id;
}

public String getImage_url() {
    return image_url;
}

public void setImage_url(String image_url) {
    this.image_url = image_url;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getImage_thumb() {
    return image_thumb;
}

public void setImage_thumb(String image_thumb) {
    this.image_thumb = image_thumb;
}

这是我的一些适配器:

public void onBindViewHolder(final ViewHolder holder, int position) {

    String desc_data = blog_list.get(position).getDesc();
    holder.setDescText(desc_data);//this works

    String image_url = blog_list.get(position).getImage_url();
    holder.setBlogImage(image_url);//this works

    String user_id = blog_list.get(position).getUser_id();
    firebaseDatabase.child("Users").child(user_id).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
                String userName = dataSnapshot.child("name").getValue().toString();

                holder.setUserName(userName);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    public void setUserName(String name){
        blogUserName = mView.findViewById(R.id.blog_user_name);
        blogUserName.setText(name);
    }

我基本上想做的是在user_id中查找名称并在TextView

中检索它

4 个答案:

答案 0 :(得分:7)

为了使其正常工作,我建议您在模型类和代码中进行一些更改。您的模型类应如下所示:

public class BlogPost {
    public String imageThumb, userId, imageUrl, desc;

    public BlogPost() {}

    public BlogPost(String imageThumb, String userId, String imageUrl, String desc) {
        this.imageThumb = imageThumb;
        this.userId = userId;
        this.imageUrl = imageUrl;
        this.desc = desc;
    }

    public String getImageThumb() {return imageThumb;}
    public String getUserId() {return userId;}
    public String getImageUrl() {return imageUrl;}
    public String getDesc() {return desc;}
}

请参阅字段和getter的命名约定。

  

为了使其有效,请不要忘记删除旧数据并添加新数据。

假设您的活动的.XML文件包含RecyclerView,如下所示:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_view"/>

项目文件的.XML文件,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image_thumb_text_view" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/user_id_text_view" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/image_url_text_view" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/desc_text_view" />
</LinearLayout>

要使用RecyclerViewFriebaseRecyclerAdapter中显示您的数据,请按照以下步骤操作:

首先,您需要在活动中找到RecyclerView并设置LinearLayoutManager,如下所示:

RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

然后,您需要创建Firebase数据库的根引用和Query对象,如下所示:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Users");

然后你必须像这样创建一个FirebaseRecyclerOptions对象:

FirebaseRecyclerOptions<BlogPost> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<BlogPost>()
        .setQuery(query, BlogPost.class)
        .build();

在您的活动类中,创建一个如下所示的holder类:

private class BlogPostHolder extends RecyclerView.ViewHolder {
    private TextView imageThumbtextView, userIdTextView, imageUrlTextView, descTextView;

    BlogPostHolder(View itemView) {
        super(itemView);
        imageThumbtextView = itemView.findViewById(R.id.image_thumb_text_view);
        userIdTextView = itemView.findViewById(R.id.user_id_text_view);
        imageUrlTextView = itemView.findViewById(R.id.image_url_text_view);
        descTextView = itemView.findViewById(R.id.desc_text_view);
    }

    void setBlogPost(BlogPost blogPost) {
        String imageThumb = blogPost.getImageThumb();
        imageThumbtextView.setText(imageThumb);
        String userId = blogPost.getUserId();
        userIdTextView.setText(userId);
        String imageUrl = blogPost.getImageUrl();
        imageUrlTextView.setText(imageUrl);
        String desc = blogPost.getDesc();
        descTextView.setText(desc);
    }
}

然后创建一个声明为global的适配器:

private FirebaseRecyclerAdapter<BlogPost, BlogPostHolder> firebaseRecyclerAdapter;

并在你的活动中实例化它:

firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<BlogPost, BlogPostHolder>(firebaseRecyclerOptions) {
    @Override
    protected void onBindViewHolder(@NonNull BlogPostHolder blogPostHolder, int position, @NonNull BlogPost blogPost) {
        blogPostHolder.setBlogPost(blogPost);
    }

    @Override
    public BlogPostHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);

        return new BlogPostHolder(view);
    }
};
recyclerView.setAdapter(firebaseRecyclerAdapter);

最后,不要忘记覆盖以下两种方法并开始侦听更改:

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

@Override
protected void onStop() {
    super.onStop();

    if (firebaseRecyclerAdapter!= null) {
        firebaseRecyclerAdapter.stopListening();
    }
}

答案 1 :(得分:1)

最好不要在ViewBinder中包含任何firebase调用以进行回收站视图。 您可以做的是更新您的BlogPost并包含带有getter和setter的字段名称。然后在您将ActivityPost添加到回收器适配器的活动中,您可以获取用户名,将名称添加到博客帖子并通知适配器数据更改。

在您的活动中执行此操作

// adding BlogPost to list
// Create BlogPost object with the data.
blogList.add(blogPostObject)
firebaseDatabase.child("Users").child(user_id).child("name").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists()){
              blogPostObject.setName(dataSnapshot.getValue().toString());
              adapter..notifyItemChanged(indexOfblogPostObject);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

答案 2 :(得分:1)

下面的代码在此行下面的onBindviewhoder()内部:

    String user_id = . . .. . . . 

我猜它是你引用数据的方式以及你附加监听器的方式。尝试初始化数据库引用,如下所示:

  DatabaseReference firebaseDatabase= FirebaseDatabase.getInstance().getReference().child("Users").child(user_id);

现在尝试这样听:

   firebaseDatabase.addValueEventListener(new ValueEvent....{

    onDataChange(.....){

     //your same code

    }



   })

答案 3 :(得分:0)

 public void checklocation() {


        recyclerView=findViewById(R.id.recy);
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        
        databaseReference = database.getReference();

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
                for (DataSnapshot snapshot1: snapshot.getChildren()){
                   Model_lats model_lats=snapshot1.child("location").getValue(Model_lats.class);
                    latslist.add(model_lats);

                }
                adapter_lats=new Adapter_lats(MapsActivity.this,latslist);
                recyclerView.setAdapter(adapter_lats);
                adapter_lats.notifyDataSetChanged();

            }

            @Override
            public void onCancelled(@NonNull @NotNull DatabaseError error) {
                Toast.makeText(MapsActivity.this, "cant read"+error, Toast.LENGTH_SHORT).show();
            }
        });
    }