【问题标题】:Room - only allow 1 item to have boolean set to trueRoom - 仅允许 1 个项目将布尔值设置为 true
【发布时间】:2018-04-18 15:08:12
【问题描述】:

使用带有实时数据的房间...我想不通的一件事是:

object1.isSelected == true;
object2.isSelected == false;
object3.isSelected == false;

如果我在 object3 上将 'isSelected' 设置为 true,我希望它在 object 1 中变为 false。

有没有办法在不观察两个对象、进行切换然后更新两个对象的情况下做到这一点?

【问题讨论】:

    标签: android observable android-room android-livedata


    【解决方案1】:

    你可以通过创建一个类来做到这一点。

    public class OneBoolean{
        private Object[] objects;
    
        public OneBoolean(Object objects..){
            this.objects = objects;
        }
    
        private void setSelected(int index){
            // This is first approach. You can store the previous selected index and just change it false, and change this true
            for(int i=0 ; i<objects.length() ; i++){
                objects[i].isSelected = (i==index);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 1970-01-01
      相关资源
      最近更新 更多