【发布时间】: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