滚动ListView上的重复项

时间:2018-08-06 08:31:03

标签: android listview firebase-realtime-database scrollview

我有一个bookList的listView,当我实现滚动listView时我有重复的Book,我尝试了几种解决方案,但是,我不知道我的问题在哪里:

例如:

  • 在我的列表视图中,我第一次显示5本书:本书1,本书
    2,第3本书,第4本书,第5本书。
  • 当我在列表上滚动时我应该拥有:本书1,本书2,本书3,本书 4、5、6、7书。

  • 但我有:第1本书,第2本书,第3本书,第4本书,第5本书,第1本书, 第6书。

  • 当我再次滚动时,我有:书1,书2,书3,书 4,第5本书,第1本书,第6本书,第6本书,第7本书。

FeedListAdapter代码:

      public FeedListAdapter(Context context, List<Book> feedItems) {
                    this.context = context;
                    this.feedItems = feedItems;
                    this.mStringFilterList=feedItems;     
                }
     @Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    final int pos = position;


    if (inflater == null)
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewHolder holder;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.feed_item, null);
        holder = new ViewHolder();
        holder.profilePic = (ImageView) convertView.findViewById(R.id.book_picture_display);
        holder.name = (TextView) convertView.findViewById(R.id.name_book_display);
        holder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);
        holder.statusMsg = (TextView) convertView.findViewById(R.id.desc_display);
        holder.prix = (TextView) convertView.findViewById(R.id.prix_display);
        holder.ville = (TextView) convertView.findViewById(R.id.ville_display);
        holder.typeCat = (TextView) convertView.findViewById(R.id.type_ann_to_display);
        holder.catLiv=(TextView)convertView.findViewById(R.id.catann_display_myBook);

        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }




    storageDisplayImg = FirebaseStorage.getInstance();

    mFirebaseInstance = FirebaseDatabase.getInstance();
    mFirebaseDatabase = mFirebaseInstance.getReference("books");


    Book item = feedItems.get(position);

    idLivreListAdapter=item.getKeyIdNode();
    idUtilisateur=item.getId_user_fav();

    if (!TextUtils.isEmpty(item.getNom_livre())) {
        holder.name.setText(item.getNom_livre());
        holder.name.setVisibility(View.VISIBLE);
    } else {
        // status is empty, remove from view
        holder.name.setVisibility(View.GONE);
    }

    if (!TextUtils.isEmpty(item.getDate_creation())) {
        holder.timestamp.setText(item.getDate_creation());
        holder.timestamp.setVisibility(View.VISIBLE);
    } else {
        // status is empty, remove from view
        holder.timestamp.setVisibility(View.GONE);
    }

    // Chcek for empty status message
    if (!TextUtils.isEmpty(item.getDesc_livre())) {
        holder.statusMsg.setText(item.getDesc_livre());
        holder.statusMsg.setVisibility(View.VISIBLE);
    } else {
        holder.statusMsg.setVisibility(View.GONE);
    }

    if (!TextUtils.isEmpty(item.getType_annonce_selected())) {
        holder.typeCat.setText(item.getType_annonce_selected());
        holder.typeCat.setVisibility(View.VISIBLE);
    } else {
        holder.typeCat.setVisibility(View.GONE);
    }

    if (!TextUtils.isEmpty(item.getCat_selected())) {
        item.getCat_selected();
        holder.catLiv.setText(item.getCat_selected());
        holder.catLiv.setVisibility(View.VISIBLE);
    } else {
        holder.catLiv.setVisibility(View.GONE);
    }


    if (item.getPrix_livre() != null) {
        holder.prix.setText(item.getPrix_livre() + " DH");
        holder.prix.setVisibility(View.VISIBLE);
    } else {
        holder.prix.setVisibility(View.GONE);
    }

    if (item.getVille_livre() != null) {

        holder.ville.setText(item.getVille_livre());
        holder.ville.setVisibility(View.VISIBLE);
    } else {
        holder.ville.setVisibility(View.GONE);
    }

    storageRef = storageDisplayImg.getReferenceFromUrl(item.getChemin_image());

    Log.e("storageRef ", "storageRef " + storageRef.toString());


    if (item.getChemin_image() != null ) {


        Glide.with(convertView.getContext())
                .using(new FirebaseImageLoader())
                .load(storageRef)
                .into( holder.profilePic);
        holder.profilePic.setVisibility(View.VISIBLE);

    } else {
        holder.profilePic.setVisibility(View.GONE);
    }

    return convertView;
}

我使用ListView的片段:

listView.setOnScrollListener(new AbsListView.OnScrollListener() {

       private int currentVisibleItemCount;
       private int currentScrollState;
       private int currentFirstVisibleItem;
       private int totalItem;

       @Override
       public void onScrollStateChanged(AbsListView view, int scrollState) {
           int first = view.getFirstVisiblePosition();
           int count = view.getChildCount();

           if (scrollState == SCROLL_STATE_IDLE || (first + count > listAdapter.getCount()) ) {
               listView.invalidateViews();
           }

           this.currentScrollState = scrollState;
           this.isScrollCompleted();


       }

       @Override
       public void onScroll(AbsListView view, int firstVisibleItem,
                            int visibleItemCount, int totalItemCount) {

           this.currentFirstVisibleItem = firstVisibleItem;
           this.currentVisibleItemCount = visibleItemCount;
           this.totalItem = totalItemCount;


       }

      private void isScrollCompleted() {
      if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                   && this.currentScrollState == SCROLL_STATE_IDLE) {


               ref.orderByChild("date_creation").startAt(timeToDisplayLastNode).limitToFirst(2).addListenerForSingleValueEvent(new ValueEventListener() {
                   @Override
                   public void onDataChange(DataSnapshot dataSnapshot) {

                                           List<String> list = new ArrayList<String>();
                       for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {

                           Book valueBook = dataSnap.getValue(Book.class);
                           keyId = dataSnap.getKey();

                           String titreLivreToDisplay = valueBook.getNom_livre();
                           String descLivreToDisplay = valueBook.getDesc_livre();

                           valueBook.setNom_livre(titreLivreToDisplay);
                           valueBook.setDesc_livre(descLivreToDisplay);
                           valueBook.setKeyIdNode(keyId);
                           feedItems.add(valueBook);
                       }

                    //Collections.reverse(feedItems);
                       listAdapter.notifyDataSetChanged();

                   } });  }}  });

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

理想情况下,视图将被回收。如果您未将文本设置为特定位置的项目,则仍可以显示旧数据。因此,最好为每个项目设置数据。即使您没有必需的数据,也最好设置应该可见的默认数据。

onclick="alert('null');"