使用 RecyclerView 隐藏列表模型中的重复项

Posted

技术标签:

【中文标题】使用 RecyclerView 隐藏列表模型中的重复项【英文标题】:Hide Duplicate item in List Model with RecyclerView 【发布时间】:2021-12-16 18:41:02 【问题描述】:

当我想在 itemview 中显示 3 个项目中的 1 个时遇到问题,这 3 个项目是我从 sqlite 调用的。 有没有办法在不改变数据库的情况下解决这个问题?

有没有办法显示我在下图中用红色标记的最后 1 个数据?

更新问题

我使用下面的方法获取数据,我试过了,它可以工作,没有重复数据,但是当我运行应用程序时它停止工作。

public List<HistoryFilesModel> readTableMediaInfo()
    ArrayList<HistoryFilesModel> historyList = new ArrayList<>();

    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT  * FROM " + TABLE_MEDIA_INFO + " ORDER BY " + COL_TAKEN_AT_TIMESTAMP_IN_SECONDS + " DESC";

    Cursor cursor = db.rawQuery(query, null);

    if (cursor.moveToFirst()) 
        do 
            HistoryFilesModel main = new HistoryFilesModel();
            main.setId(cursor.getString(cursor.getColumnIndex(COL_ID)));
            main.setUrl(cursor.getString(cursor.getColumnIndex(COL_URL)));
            main.setFilePath(cursor.getString(cursor.getColumnIndex(COL_FILE_PATH)));
            main.setFilename(cursor.getString(cursor.getColumnIndex(COL_FILE_NAME)));
            main.setUserId(cursor.getInt(cursor.getColumnIndex(COL_USER_ID)));
            main.setUsername(cursor.getString(cursor.getColumnIndex(COL_USERNAME)));
            main.setFullName(cursor.getString(cursor.getColumnIndex(COL_FULL_NAME)));
            main.setThumbnail(cursor.getString(cursor.getColumnIndex(COL_THUMBNAIL)));
            main.setProfilePictUrl(cursor.getString(cursor.getColumnIndex(COL_URL_DISPLAY_PICT)));
            main.setCaption(cursor.getString(cursor.getColumnIndex(COL_CAPTION)));
            main.setVideo(cursor.getString(cursor.getColumnIndex(COL_TYPE)).startsWith("video"));
            main.setTotalLength(cursor.getInt(cursor.getColumnIndex(COL_TOTAL_LENGTH)));
            if (!historyList.contains(main))
                historyList.add(main); // ERROR WITH THIS 
            


         while (cursor.moveToNext());
    

    return historyList;

这个错误在下面

2021-11-04 14:27:11.829 5588-5588/com.sweet.cloves.instasaver E/androidRuntime: FATAL EXCEPTION: main
Process: com.sweet.cloves.instasaver, PID: 5588
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolderb4fdd1 position=2 id=-1, oldPos=0, pLpos:0 scrape [attachedScrape] tmpDetached no parent androidx.recyclerview.widget.RecyclerViewca05af0 VFED..... ......I. 0,0-1080,1634 #7f0a0193 app:id/rv_history_content, adapter:com.sweet.cloves.instasaver.adapter.HistoryFragmentAdapter@19c0869, layout:androidx.recyclerview.widget.LinearLayoutManager@435c2ee, context:com.sweet.cloves.instasaver.view.activity.MainActivity@b4c0932
    at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5974)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6158)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4085)
    at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3534)
    at android.view.View.measure(View.java:27131)
    at androidx.core.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1599)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at androidx.core.widget.NestedScrollView.onMeasure(NestedScrollView.java:585)
    at android.view.View.measure(View.java:27131)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
    at android.view.View.measure(View.java:27131)
    at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1638)
    at android.view.View.measure(View.java:27131)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7951)
    at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:760)
    at com.google.android.material.appbar.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:99)
    at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:2003)
    at androidx.coordinatorlayout.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:831)
    at android.view.View.measure(View.java:27131)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
    at android.view.View.measure(View.java:27131)
    at androidx.drawerlayout.widget.DrawerLayout.onMeasure(DrawerLayout.java:1119)
    at android.view.View.measure(View.java:27131)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7951)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
    at android.view.View.measure(View.java:27131)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7951)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
    at android.view.View.measure(View.java:27131)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7951)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
    at android.view.View.measure(View.java:27131)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:7951)

无论你的回答是什么,我都很感激

【问题讨论】:

【参考方案1】:

如果我明白你想要做什么......

您可以使用distinctBy() 删除重复项

YourList.distinctBy 
            it.WHAT_EVER_YOU_WANT_TO_DISTINCT_BY // maybe by monkeysquad
        )

这将为您提供从 SQL 查询中获得的列表,没有重复项

希望我做对了

【讨论】:

有例子吗? kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/…

以上是关于使用 RecyclerView 隐藏列表模型中的重复项的主要内容,如果未能解决你的问题,请参考以下文章

在recyclerview中使用textureview播放视频

在RecyclerView列表滚动的时候显示或者隐藏Toolbar

在 RecyclerView/ListView onScroll 上隐藏 ActionBar

RecyclerView的行项目视图隐藏/显示在Recyclerview的滚动上搞砸了

如何通过firebase实时数据库永久隐藏RecyclerView中的imageview

尝试隐藏具有空属性的卡片时,如何消除 recyclerView 中的空白?