数据更改时重复查看?

时间:2016-06-05 09:57:26

标签: android-studio android-recyclerview android-cardview

我的CardView在数据更改时重复元素,vardView在一个选项卡中,以及我声明该选项卡片段的方式如下;

在onCreateView中,我声明了所有必要的firebase链接和值事件监听器,以检索与卡上显示的元素相关的所需数据。

ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            if(snapshot !=null){
                for (DataSnapshot child: snapshot.getChildren()) {
                    Log.i("MyTag", child.getValue().toString());
                    imagesfeedsList.add(child.child("address").getValue(String.class));
                    authorfeedsList.add(child.child("author").getValue(String.class));
                    ratingfeedsList.add(child.child("rating").getValue(String.class));
                    locationfeedsList.add(child.child("location").getValue(String.class));
                    publicIDfeedsList.add(child.child("public_id").getValue(String.class));

                }
                Log.i("MyTag_imagesDirFinal", imagesfeedsList.toString());

                mImages = imagesfeedsList.toArray(new String[imagesfeedsList.size()]);
                author = authorfeedsList.toArray(new String[authorfeedsList.size()]);
                ratingV = ratingfeedsList.toArray(new String[ratingfeedsList.size()]);
                locationV = locationfeedsList.toArray(new String[locationfeedsList.size()]);
                publicID = publicIDfeedsList.toArray(new String[publicIDfeedsList.size()]);

                numbOfAdrs = Long.valueOf(imagesfeedsList.size());
                LENGTH = Integer.valueOf(String.valueOf(numbOfAdrs));
            }

在代码片段之后的适配器设置;

    ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
    recyclerView.setAdapter(adapter);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    return recyclerView;
}

然后是带有RecycleView的视图持有者,声明了cardView元素。其中一个元素是ratingBar,这里评级栏Listener将在特定图片上提交用户评级。

之后的内容适配器;

 public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
    // Set numbers of List in RecyclerView.

    private Context mContext;

    public ContentAdapter(Context context) {

        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
    }

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

        holder.authorName.setText(author[position]);

        holder.ratingValue.setText(ratingV[position]);

        holder.locationValue.setText(locationV[position]);

        Picasso.with(mContext).load(mImages[position]).into(holder.picture);
  @Override
    public int getItemCount() {
        return LENGTH;
    }
}

我的问题是,无论何时用户提交评级,或者甚至当与任何卡上的任何元素相关的数据发生变化时,视图都会重复(我的意思是视图,卡片),重复的卡片,以及新数据显示?

我不确定我上面的代码结构是什么导致这个以及如何修复这个重复,我的意思是我需要使用新数据更新卡但不重复?

1 个答案:

答案 0 :(得分:0)

好吧,问题是每次数据发生变化时,在onCreate中,imagesFeedList,ratingFeedList等都没有从初始构建中删除存储在其中的旧信息,所以当onDataChange触发刷新时,新信息被添加到以前的信息中,这导致视图重复卡片,因此只是在onDataChange的开头,在将信息存储到多个feedLists之前,必须将其清除;

   imagesfeedsList.clear();
   authorfeedsList.clear();
   ratingfeedsList.clear();
   locationfeedsList.clear();
   publicIDfeedsList.clear(); 

并且我确保视图不会根据旧信息重复构建。

相关问题