【问题标题】:duplicate view upon data change?数据更改时的重复视图?
【发布时间】:2016-10-05 01:37:27
【问题描述】:

我的 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));
            }

在 sn-p 适配器设置之后;

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

    return recyclerView;
}

然后是带有 RecycleView 的视图持有者,声明了 cardView 元素。其中一个元素是 ratingBar,这里 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;
    }
}

我的问题是,每当用户提交评分时,甚至当与 anycard 上的任何元素相关的数据发生更改时,视图都会重复(我的意思是视图,卡片),卡片的重复,与显示新的数据变化?

我不确定我上面的代码结构中是什么导致了这种情况以及如何解决这种重复,我的意思是我需要用新数据更新卡片但不重复?

【问题讨论】:

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


    【解决方案1】:

    好吧,所以问题是每次数据发生变化时,在onCreate中,imagesFeedList、ratingFeedList等并没有摆脱最初构建时存储在其中的旧信息,所以当刷新发生时由onDataChange触发, 新信息被添加到之前的信息中,导致视图重复卡片,因此在 onDataChange 的开头以及在几个 feedLists 中存储任何信息之前,必须将其清除;

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

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

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 1970-01-01
      • 2015-11-14
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      • 2022-11-22
      相关资源
      最近更新 更多