简体   繁体   English

Android Firebase自定义RecyclerView项目单击问题

[英]Android Firebase custom RecyclerView item click issue

I have to use custom RecyclerView because I don't want to update to list real time. 我必须使用自定义RecyclerView,因为我不想更新以列出实时数据。

How do I get an id if I want to go into the details of the data? 如果要查看数据的详细信息,如何获取ID? As in FirebaseRecyclerAdapter. 与FirebaseRecyclerAdapter中一样。

  final String uid = getRef(position).getKey();

I added postId , my posts table, and I wrote the following code. 我添加了postIdposts表,并编写了以下代码。 But when click on the image, it goes to the last added image to the list. 但是,当单击图像时,它将转到列表中最后添加的图像。 And when I click upVote, every item goes crazy and they click upVote too. 当我单击upVote时,每个项目都会发疯,他们也单击了upVote。

First, am I on the right track to update the list only when I want to? 首先,我是否只有在需要时才更新列表? Second, why is everything going crazy? 第二,为什么一切都变得疯狂?

PostAdapter 后适配器

public PostRecyclerAdapter(Context context, Query query) {
    this.context = context;
    this.query = query;

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            posts.clear();
            for (DataSnapshot data : dataSnapshot.getChildren()) {
                posts.add(data.getValue(Post.class));
            }
            Collections.sort(posts, new Comparator<Post>() {
                @Override
                public int compare(Post o1, Post o2) {
                    Long a = o1.getCreatedDate();
                    Long b = o2.getCreatedDate();
                    if (a < b) {
                        return -1;
                    } else if (a == b) {
                        return 0;
                    } else {
                        return 1;
                    }
                }
            });
            notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

 @Override
public void onBindViewHolder(final PostViewHolder viewHolder, int position) {

    model = posts.get(position);
    postId = model.getPostId();
    viewHolder.setTitle(model.getTitle());
    viewHolder.setImage(context, model.getImage());
    viewHolder.setUpVote(postId);

     viewHolder.imvImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, SinglePostActivity.class);
            intent.putExtra(Enums.PostKeys.postId.getValue(), postId);
            context.startActivity(intent);
        }
    });

 viewHolder.imbUpVote.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (!checkAuthUser()) {
                context.startActivity(new Intent(context, SignUpActivity.class));
                return;
            }
            processVote = true;

            Singleton.getDbPostDownVote(postId).child(postId).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (processVote == true) {
                        if (dataSnapshot.hasChild(getUserId())) {
                            Singleton.getDbPostDownVote(postId).child(postId).child(getUserId()).removeValue();
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

            Singleton.getDbPostUpVote(postId).child(postId).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {


                    if (processVote == true) {
                        if (dataSnapshot.hasChild(getUserId())) {
                            Singleton.getDbPostUpVote(postId).child(postId).child(getUserId()).removeValue();
                            processVote = false;
                        } else {
                            Singleton.getDbPostUpVote(postId).child(postId).child(getUserId()).setValue(0);
                            processVote = false;
                        }
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    });

}

PostViewHolder:setUpVote PostViewHolder:setUpVote

public void setUpVote(final String postId) {
    Singleton.getDbPostUpVote(postId).child(postId).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.hasChild(getUid())) {
                imbUpVote.setImageResource(R.drawable.vote_up_active);
            } else {
                imbUpVote.setImageResource(R.drawable.vote_up_passive);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

How do I get an id if I want to go into the details of the data? 如果要查看数据的详细信息,如何获取ID?

Usually the id is a node in your db. 通常,id是您数据库中的一个节点。 As you can see in 如您所见

final String uid = getRef(position).getKey();

getKey returns tha value of the node in db. getKey返回db中节点的值。 In your case to avoid sorting the list with comparator i would just structure the data like so: 在您的情况下,为避免使用比较器对列表进行排序,我将像这样构造数据:

20170111
  title : some title
  text : some text
20170112
  title : some title
  text : some text

This way data is going to be sorted by the nodes, which is the date, by Firebase. 这样,数据将由Firebase按节点(即日期)排序。 If you want to be more precise you can also add hours and minutes. 如果要更精确,还可以增加小时和分钟。

First, am I on the right track to update the list only when I want to? 首先,我是否只有在需要时才更新列表?

No. 没有。

Calling addValueEventListener() is going to trigger the code inside the listener each time the value in your db changes. addValueEventListener()数据库中的值更改时,调用addValueEventListener()都会触发侦听器内的代码。 In other words, its realtime. 换句话说,它是实时的。 Use addListenerForSingleValueEvent() insted. 使用addListenerForSingleValueEvent() It fires only once. 它只发射一次。

Second, why is everything going crazy? 第二,为什么一切都变得疯狂?

Very important thing about onDataChange() is that it fires not only when the value changes but also the first time you set the listener. 关于onDataChange()非常重要的一点是,它不仅会在值更改时触发,还会在您首次设置侦听器时触发。 That is why everything is getting voted up when you click one item. 这就是为什么当您单击一项时,所有内容都会被投票通过的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM