簡體   English   中英

如何從Android中的數組列表中獲取不同的值

[英]How to get distinct values from array list in Android

在 MySql 數據庫中,我有一個包含更多列的產品表:

類別,型號,顏色,尺寸,價格。

SplashActivity上的 Android 應用程序中,我從產品表中獲取數據,將其保存在一個常量ArrayList並用所有產品列表填充RecyclerView

我怎樣才能從這個數組列表中獲得不同的類別、不同的模型、不同的顏色? 我想使用 Dropbox 中的那些來過濾產品列表。

這就是我從 Mysql 表中獲取數據並將其保存在Constant.allProducts

            call.enqueue(new Callback<ProductMain>() {
                @Override
                public void onResponse(Call<ProductMain> call, Response<ProductMain> response) {
                    ProductMain main=response.body();
                    Constants.toateProdusele.clear();
                    Constants.toateProdusele.addAll(main.getProduse())
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            startActivity(new Intent(SplashActivity.this,HomePageActivity.class));
                            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                            finish();
                        }
                    },1500);
                }

                @Override
                public void onFailure(Call<ProductMain> call, Throwable t) {
                    Toast.makeText(SplashActivity.this,""+t,Toast.LENGTH_LONG).show();

                }

            });
//and i populate the recyclerview
        arrayList = new ArrayList<>();
        arrayList.addAll(Constants.toateProdusele);
        adapter= new ProduseAdapter(HomePageActivity.this,  arrayList, ProductDetailActivity.class) ;
        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();

歡迎來到 stackoverflow。

而不是將其保存在ArrayList<>中,而是使用HashSet<>只保存唯一值,

如果列表包含在您的代碼中定義的類的對象,您將必須覆蓋該類中的equals()方法以區分哪些對象彼此不同以及hashcode()方法

這是HashSet一個正確示例,它將為您提供與ArrayList不同的值

public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1,2,2,2,3,5);

    System.out.println(numbers);

    Set<Integer> hashSet = new LinkedHashSet(numbers);
    ArrayList<Integer> removedDuplicates = new ArrayList(hashSet);

    System.out.println(removedDuplicates);
}

暫無
暫無

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

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