android - 如何使用 Switch 基于字符串对 RecyclerView 中的 CardView 进行排序?

标签 android list sorting arraylist android-recyclerview

我想按字母顺序或受欢迎程度排序一个 RecyclerView 充满基于开关的 cardViews,这是我到目前为止所做的:

 public class CategoriesRecyclerView extends Activity {
            private List<Categories> categories;
            private RecyclerView rv;
            private Switch categoriesSortingSwitch;
            private TextView switchStatus;



@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


            setContentView(R.layout.categories_recycler_view);


            rv = (RecyclerView) findViewById(R.id.cardList);
            rv.setHasFixedSize(true);
            LinearLayoutManager llm = new LinearLayoutManager(this);
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            rv.setLayoutManager(llm);

            //HERE I CALL THE METHOD THAT INITIALIZE ALL THE CARDVIEWS WITH IT'S ELEMENTS ISIDE THE RECYCLERVIEW
            initializeData();

            CategoriesAdapter ca = new CategoriesAdapter(categories);
            rv.setAdapter(ca);


            //SWITCH IMPLEMENTATION FOR THE SORTING
            categoriesSortingSwitch = (Switch) findViewById(R.id.switchsortcategories);
            switchStatus = (TextView) findViewById(R.id.testswitch);
            //set the switch to OFF
            categoriesSortingSwitch.setChecked(false);
            //attach a listener to check for changes in state
            categoriesSortingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {



                       if(isChecked){
    //HERE I SHOW A TEXT VIEW WITH THE SWITCH STATUS (WORKS FINE)
                            switchStatus.setText("Sorting alphabetically");



    //HERE I TRY TO ADD ELEMENTS TO THE LIST categories ORGANIZED BY THEIR NAME

                        categories.add(new Categories("CARS", "CARS"));
                        categories.add(new Categories("COUSINE", "COUSINE"));
                        categories.add(new Categories("GAMBLING", "GAMBLING"));
                        categories.add(new Categories("GAMING", "GAMING"));
                        categories.add(new Categories("HISTORY", "HISTORY"));
                        categories.add(new Categories("MUSIC", "MUSIC"));
                        categories.add(new Categories("NATURE", "NATURE"));
                        categories.add(new Categories("RANDOM", "RANDOM"));
                        categories.add(new Categories("SPORTS", "SPORTS"));
                        categories.add(new Categories("STUDIES", "STUDIES"));
                        categories.add(new Categories("TECH", "TECH"));




                    }else{
                        switchStatus.setText("Sorting by popularity");


     //HERE I TRY TO ADD ELEMENTS TO THE LIST categories ORGANIZED BY THEIR POPULARITY


                        categories.add(new Categories("CARS", "CARS"));
                        categories.add(new Categories("SPORTS", "SPORTS"));
                        categories.add(new Categories("GAMING", "GAMING"));
                        categories.add(new Categories("GAMBLING", "GAMBLING"));
                        categories.add(new Categories("TECH", "TECH"));
                        categories.add(new Categories("NATURE", "NATURE"));
                        categories.add(new Categories("RANDOM", "RANDOM"));
                        categories.add(new Categories("COUSINE", "COUSINE"));
                        categories.add(new Categories("HISTORY", "HISTORY"));
                        categories.add(new Categories("MUSIC", "MUSIC"));
                        categories.add(new Categories("STUDIES", "STUDIES"));
                    }

                }
            });


            //check the current state before we display the screen
            if(categoriesSortingSwitch.isChecked()){
                switchStatus.setText("Sorting alphabetically");



                categories.add(new Categories("CARS", "CARS"));
                categories.add(new Categories("COUSINE", "COUSINE"));
                categories.add(new Categories("GAMBLING", "GAMBLING"));
                categories.add(new Categories("GAMING", "GAMING"));
                categories.add(new Categories("HISTORY", "HISTORY"));
                categories.add(new Categories("MUSIC", "MUSIC"));
                categories.add(new Categories("NATURE", "NATURE"));
                categories.add(new Categories("RANDOM", "RANDOM"));
                categories.add(new Categories("SPORTS", "SPORTS"));
                categories.add(new Categories("STUDIES", "STUDIES"));
                categories.add(new Categories("TECH", "TECH"));


            }
            else {
                switchStatus.setText("Sorting by popularity");






                categories.add(new Categories("CARS", "CARS"));
                categories.add(new Categories("SPORTS", "SPORTS"));
                categories.add(new Categories("GAMING", "GAMING"));
                categories.add(new Categories("GAMBLING", "GAMBLING"));
                categories.add(new Categories("TECH", "TECH"));
                categories.add(new Categories("NATURE", "NATURE"));
                categories.add(new Categories("RANDOM", "RANDOM"));
                categories.add(new Categories("COUSINE", "COUSINE"));
                categories.add(new Categories("HISTORY", "HISTORY"));
                categories.add(new Categories("MUSIC", "MUSIC"));
                categories.add(new Categories("STUDIES", "STUDIES"));
            }

        }

    //HERE IS THE initializeData() METHOD THAT I CALLED BEFORE THAT HAS A NEW LIST THAT SUPPOSEDLY WAS FILLED ALPHABETICALLY OR BY POPULARITY BASED ON THE SWITCH STATUS.

        private void initializeData() {


            categories = new ArrayList<>();

        }
    }

此时它编译并运行,当开关状态更改时,它会在我为此创建的 TextView 上正确地通知它,但是 RecyclerView 的元素没有任何反应,它们只是出于某种原因保持相同的顺序.

基本上想要做的是按照开关指定的顺序直接将元素添加到在方法 initializeData() 中创建的列表中。

我怎样才能按照我想要的方式进行这项工作? 我没有使用排序算法,因为数据很小。

最佳答案

您可能想使用 Comparator对您的 Categories 进行排序,然后将排序列表设置为为您的 CardView 供电的适配器s,然后用 notifyDataSetChanged() 通知适配器.

This tutorial将有助于如何使用 Comparator

关于android - 如何使用 Switch 基于字符串对 RecyclerView 中的 CardView 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32426755/

相关文章:

r - 测试矩阵或数据框的行是否在 R 中排序

android - fragment 中的 onBackPressed()

android - 在分享 Intent android 中使用 Facebook sdk 的 ShareContent

python - 您可以在 python 中手动对列表进行排序吗?

list - Flutter:我如何在列表中实现列表?错误:只能在initializers.dart中访问静态成员(implicit_this_reference_in_initializer)

java - Java 6 中有哪些不同的排序算法可用?

c# - 在 C# 中对字符串进行排序

java - 如何更改BottomSheetDialogFragment的BottomSheetBehavior?

android - 为视频流实现 float View

python - 如何将字符串 append 到 float 列表