MainActivity和MVVM中的公开列表

问题描述

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

这是我的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;
                    }
                }

            }
        }
    });

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)