2015-06-30 70 views
0

我试图使用this library来实现MaterialDialog中的复选框,并且复选框会询问用户是否不想再次看到该对话框。如果用户的手机具有NFC,则会出现该对话框,但该对话框已停用。当对话框按钮被按下时复选框状态不被传递

如果用户按下对话框中的肯定按钮并勾选了方框,则它会使用名为“NfcStatus”的布尔属性访问Realm对象,并将其设置为true。如果他们按下带有方框的负数按钮,则该Realm对象的NfcStatus被设置为false。

这里的MaterialDialog的代码:

new MaterialDialog.Builder(context) 
      .title("NFC") 
      .content("NFC is disabled. Would you like to activate it?") 
      .items(R.array.checkbox) //this just has one string in it which says "Please don't show me this again" 
      .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() { 
       @Override 
       public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) { 
        /** 
        * If you use alwaysCallMultiChoiceCallback(), which is discussed below, 
        * returning false here won't allow the newly selected check box to actually be selected. 
        * See the limited multi choice dialog example in the sample project for details. 
        **/ 
        checkboxIsChecked = true; //TODO: checkboxIsChecked isn't being passed into onPositive or onNegative 
        return true; 
       } 
      }) 
      .positiveText(R.string.accept) 
      .positiveColorRes(R.color.main_theme_color) 
      .negativeText(R.string.decline) 
      .negativeColorRes(R.color.main_theme_color) 
      .callback(new MaterialDialog.ButtonCallback() { 
       @Override 
       public void onPositive(MaterialDialog dialog) { 
        //this was how I was checking if checkboxIsChecked was true or false 
        Log.d("checkboxIsChecked", checkboxIsChecked?"true":"false"); } 

        if (checkboxIsChecked) {begins 
         if (ks.contains(KEY_NAME)) { 
          realmKey = ks.get(KEY_NAME); 
         } 
         realm = Realm.getInstance(context, realmKey); 
         RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst();      realmPhone.setNfcStatus(true);       
        } 
        activity.finish(); 
        startNfcSettingsActivity(); 

        Toast.makeText(context, R.string.nfc_disabled_message, Toast.LENGTH_LONG).show(); 
       } 

       @Override 
       public void onNegative(MaterialDialog dialog) { 
        if (checkboxIsChecked) { 
         if (ks.contains(KEY_NAME)) { 
          realmKey = ks.get(KEY_NAME); 
         } 
         realm = Realm.getInstance(context, realmKey); 
         RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst(); 
         realmPhone.setNfcStatus(false); 

        } 
       } 

      }) 
      .cancelable(false) 
      .show(); 

的问题是,即使复选框被选中,在checkboxIsChecked变量仍然是虚假的onPositive或onNegative当使用它,所以它永远不会被写入到Realm对象。我这样做是错误的吗?

回答

0

要更改和保存RealmObject,您需要使用事务。相关文档可以发现here

在你的情况下,这将是这样的:

realm = Realm.getInstance(context, realmKey); 
// I'm not quite sure how did you create the realmPhone at the first time, 
// just assume you have one realmPhone in the Realm. 
RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst(); 

realm.beginTransaction(); 
realmPhone.setNfcStatus(false); 
realm.commitTransaction(); 

// Close the realm instance after using it is very important! To avoid leaks. 
realm.close(); 

顺便说一句,似乎代码:

RealmPhone realmPhone = realm.where(RealmPhone.class).findFirst(); 
realmPhone.setNfcStatus(false); 

不叫。如果是这样,应该抛出IllegalStateException,因为你没有在Realm事务中调用它。或者,也许RealmPhone不是从RealmObject继承的?

+0

问题是,只有在“不再显示此复选框”复选框被勾选并且我按下了对话框中的任一按钮时,我才需要写入RealmPhone。我不知道如何获得该复选框的状态。 – CiaranC94

+0

好的,所以你的问题是checkboxIsChecked不能传递给不同的回调,对不对?您可以尝试将checkboxIsChecked作为Activity/Fragment /其他类的成员,并在回调中使用它们,如MyActivity.this.checkboxIsChecked。 – beeender

相关问题