Public 在 MainActivity 和 MVVM 中列出

Public List In MainActivity and MVVM

我需要一些有关 MVVM 架构的帮助。 我有一个接收 LiveData 并完美显示它的 RecyclerView,但是,我的 recyclerView 需要另一个数据源来自定义 TextView 的颜色和背景。现在我正在使用 Mainactivity 中声明的 public 列表,但我读到这不是一个好的做法。 是否可以从 RecyclerView 内部对数据库执行非实时请求,以替换 public 列表?如果没有,我真的很想得到一些建议。

这是我的 onBindViewHolder:

@Override
public void onBindViewHolder(@NonNull ResultRecyclerViewAdapter.ResultHolder holder, int position) {
       
        Results currentResult = results.get(position);
        
        holder.ston1.setText(currentResult.getSton1());
        holder.ston2.setText(String.valueOf(currentResult.getSton2()));

        holder.ston1.setBackgroundColor(0xFF12FF45);
        holder.ston2.setBackgroundColor(0xFF12FF45);

        holder.ston1.getBackground().setAlpha(100);
        holder.ston2.getBackground().setAlpha(100);

        for (Ston ston: MainActivity.Stons){
            if (currentResult.getStonCode().equals(ston.getStonCode()) && currentResult.getStonType().equals(ston.getStonType())){

                switch (ston.getStonSelected()) {
                    case "WADS":
                        holder.ston1.getBackground().setAlpha(255);
                        break;
                    case "WQAS":
                        holder.ston2.getBackground().setAlpha(255);
                        break;
                }
                break;
            }
        }


    holder.ston1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Boolean found = false;
            for (Ston ston: MainActivity.Stons){
                if (currentResult.getStonCode().equals(ston.getStonCode())){
                    found = true;
                    break;
                }
            }
            if (!found) {
                holder.ston1.getBackground().setAlpha(255);
                MainActivity.Stons.add(new Stons(currentResult.getStonCode(),"WADS",
                        currentResult.getStonType()));
            } 
            else {
                for (Ston ston : MainActivity.Stons) {
                    if (currentResult.getStonCode().equals(ston.getStonCode()) && ston.getStonSelected().equals("WADS") && 
                        ston.getStonType().equals(currentResult.getStonType())){
                        MainActivity.Stons.remove(ston);
                        holder.ston1.getBackground().setAlpha(100);
                        break;
                    }
                }

            }
        }
    });

    holder.ston2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Boolean found = false;
            for (Ston ston: MainActivity.Stons){
                if (currentResult.getStonCode().equals(ston.getStonCode())){
                    found = true;
                    break;
                }
            }
            if (!found) {
                holder.ston2.getBackground().setAlpha(255);
                MainActivity.Stons.add(new Stons(currentResult.getStonCode(),"WQAS",
                        currentResult.getStonType()));
            } 
            else {
                for (Ston ston : MainActivity.Stons) {
                    if (currentResult.getStonCode().equals(ston.getStonCode()) && ston.getStonSelected().equals("WQAS") && 
                        ston.getStonType().equals(currentResult.getStonType())){
                        MainActivity.Stons.remove(ston);
                        holder.ston2.getBackground().setAlpha(100);
                        break;
                    }
                }

            }
        }
    });

我看到的一个选项是专门为您的 recyclerview 适配器创建新类型,它将包含结果对象和您用于背景 alpha 的信息。因此,在您的 activity (或片段)中,当实时数据观察器被触发时,您不会直接将其传递给适配器,而是首先创建新类型的对象集合,然后将其传递给适配器。我强烈建议您尽可能使用 Kotlin,在那里您可以使用集合映射将集合从数据库映射到新类型的集合。