Android studio 数据更改时的重复视图?
我的CardView在数据更改时复制了元素,vardView在一个选项卡中,我声明选项卡片段的方式如下 在onCreateView中,我声明了所有必要的firebase链接和值事件侦听器,以检索与卡上显示的元素相关的所需数据Android studio 数据更改时的重复视图?,android-studio,android-recyclerview,android-cardview,Android Studio,Android Recyclerview,Android Cardview,我的CardView在数据更改时复制了元素,vardView在一个选项卡中,我声明选项卡片段的方式如下 在onCreateView中,我声明了所有必要的firebase链接和值事件侦听器,以检索与卡上显示的元素相关的所需数据 ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) {
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,在这里ratingBar侦听器将提交特定图片的用户评级
之后是内容适配器
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;
}
}
公共静态类ContentAdapter扩展了RecyclerView.Adapter{
//在RecyclerView中设置列表的编号。
私有上下文;
公共内容适配器(上下文){
this.mContext=上下文;
}
@凌驾
public ViewHolder onCreateViewHolder(视图组父级,int-viewType){
返回新的ViewHolder(LayoutInflater.from(parent.getContext()),parent);
}
@凌驾
公共无效onBindViewHolder(最终ViewHolder,内部位置){
holder.authorName.setText(作者[职位]);
holder.ratingValue.setText(ratingV[位置]);
holder.locationValue.setText(locationV[position]);
毕加索.with(mContext).load(模拟图像[位置])到(holder.picture);
@凌驾
public int getItemCount(){
返回长度;
}
}
我的问题是,每当用户提交评级,甚至当与任何卡片上的任何元素相关的数据发生变化时,视图都会重复(我的意思是视图,卡片),卡片的重复,并显示新的数据
我不确定我上面的代码结构中是什么导致了这种情况,以及如何修复这种重复,我的意思是我需要用新数据更新卡片,但不需要复制?好吧,所以问题是每次数据更改时,onCreate the Images FeedList、ratingFeedList等中都没有删除我存储的旧信息n从初始构建开始,因此当onDataChange触发刷新时,新信息会添加到以前的信息中,这会导致视图重复卡片,因此在onDataChange开始时以及在将任何信息存储到多个提要列表中之前,必须将其清除
imagesfeedsList.clear();
authorfeedsList.clear();
ratingfeedsList.clear();
locationfeedsList.clear();
publicIDfeedsList.clear();
通过这一点,我确保了视图不会重复基于旧信息的构建。好吧,所以问题是每次数据更改时,onCreate中的imagesFeedList、ratingFeedList等都不会从初始构建中删除存储在其中的旧信息,因此当onDataChange触发刷新时新信息被添加到以前的信息中,这会导致视图重复卡片,因此在onDataChange开始时,在将任何信息存储到多个FeedList中之前,必须将其清除
imagesfeedsList.clear();
authorfeedsList.clear();
ratingfeedsList.clear();
locationfeedsList.clear();
publicIDfeedsList.clear();
这样我就确保了视图不会重复基于旧信息的构建