簡體   English   中英

RecyclerView 中的項目正在獲取先前項目的數據

[英]Items in a RecyclerView are getting the data of previous items

該應用程序有一個數據庫,其中的項目具有標記為整數的位置列,顯示在 RecyclerView 中。

如果數據庫列中的任何整數為零,則不應顯示 integer,為此我添加了 if 語句來檢查每個接收到的整數,如果接收到的 integer 為零,則不應顯示被顯示。

private int journeyPosition;
...

  @Override
    public void onBindViewHolder(@NonNull JourneyAdapter.JourneyViewHolder holder, int position) {
        journeyPosition = holder.getAdapterPosition();
        JourneyObject journeyObject = journeyObjectList.get(holder.getAdapterPosition());

        if (journeyObject.getLocation1() != 0){
            holder.location1.setText(Integer.toString(journeyObject.getLocation1()));
        }
        if (journeyObject.getLocation2() != 0) {
            holder.location2.setText(Integer.toString(journeyObject.getLocation2()));
        }
        if (journeyObject.getLocation3() != 0) {
            holder.location3.setText(Integer.toString(journeyObject.getLocation3()));
        }
        if (journeyObject.getLocation4() != 0) {
            holder.location4.setText(Integer.toString(journeyObject.getLocation4()));
        }
        if (journeyObject.getLocation5() != 0) {
            holder.location5.setText(Integer.toString(journeyObject.getLocation5()));
        }

目前,如果所有位置都大於零,則代碼可以正常工作,但由於某些項目中的某些位置為零,因此這些整數是從 RecyclerView 的其他項目中獲得的,這些項目的數字大於零場。

我還沒有弄清楚為什么從以前的適配器位置接收數據。 如果有任何不清楚的地方,請說明是什么。

謝謝

請記住,這是一個回收站視圖。 滾動到屏幕上的視圖可能已被使用。 綁定視圖時,不要只使用 if 語句。 使用 if/else 語句,這樣您就可以在不想顯示任何內容時清除舊數據。

還有一個提示,當你發現自己寫了一堆看起來像復制粘貼的代碼時,找到一種通過迭代來完成它的方法。 您的代碼將更清晰、更簡潔、更易於閱讀且不易出錯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM