Java 所选值保持保存在带有单选按钮的Dialogfragment中

Java 所选值保持保存在带有单选按钮的Dialogfragment中,java,android,kotlin,Java,Android,Kotlin,我正在尝试使用其他选项在Dialogfragment中保存值。一旦保存,但如果我重新打开Dialogfragment,屏幕值将变为“无应答”。。它不会保持保存状态或调用上次保存的值。 选择“值”并保存时的第一张图像 当我重新打开对话框时 对话框 class HeightDialog : DialogFragment() { interface SingleChoiceListner { fun onPositiveButtonClicked(list: Array

我正在尝试使用其他选项在Dialogfragment中保存值。一旦保存,但如果我重新打开Dialogfragment,屏幕值将变为“无应答”。。它不会保持保存状态或调用上次保存的值。 选择“值”并保存时的第一张图像

当我重新打开对话框时

对话框


class HeightDialog : DialogFragment() {

    interface SingleChoiceListner {
        fun onPositiveButtonClicked(list: Array<String>?, positon: Int)
        fun onNegativeButtonClicked()
    }

    internal var positition = 0
    lateinit var mSingleChoiceListner: SingleChoiceListner


    override fun onAttach(context: Context) {
        super.onAttach(context)

        try {
            mSingleChoiceListner = context as SingleChoiceListner
        } catch (e: Exception) {
            throw ClassCastException(activity.toString() + "SingleChoiceListner must be implemented")
        }
    }


    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {


        val builder = AlertDialog.Builder(activity)
        val heightArray = activity?.resources?.getStringArray(R.array.height_array)

        builder.setTitle("Select Your Height")
            .setSingleChoiceItems(
                heightArray,
                positition
            ) { dialog, which -> positition=which }
            .setPositiveButton(
                "ok"
            ) { dialog, which ->
                mSingleChoiceListner.onPositiveButtonClicked(
                    heightArray,
                    positition
                )
            }
            .setNegativeButton(
                "cancle"
            ) { dialog, which -> mSingleChoiceListner.onNegativeButtonClicked() }

        return builder.create()

    }

}

类HeightDialog:DialogFragment(){
接口单选择列表器{
趣味onPositiveButtonClicked(列表:数组?,位置:Int)
fun onNegativeButtonClicked()
}
内部风险头寸=0
lateinit变量mSingleChoiceListner:SingleChoiceListner
重写转速表(上下文:上下文){
super.onAttach(上下文)
试一试{
mSingleChoiceListner=作为SingleChoiceListner的上下文
}捕获(e:例外){
抛出ClassCastException(activity.toString()+“必须实现SingleChoiceListner”)
}
}
重写FunonCreateDialog(savedInstanceState:Bundle?:对话框){
val builder=AlertDialog.builder(活动)
val heightArray=activity?.resources?.getStringArray(R.array.height\u数组)
builder.setTitle(“选择您的高度”)
.setSingleChoiceItems(
高度阵列,
位置
){dialog,which->position=which}
.setPositiveButton(
“好的”
){对话框,其中->
mSingleChoiceListner.onPositiveButtonClicked(
高度阵列,
位置
)
}
.setNegativeButton(
“癌症”
){对话框,其中->mSingleChoiceListner.onNegativeButtonClicked()}
returnbuilder.create()
}
}
活动

override fun onPositiveButtonClicked(list: Array<String>?, positon: Int) {
    val height = list?.get(positon)
    heightPosition=positon
    newHeight = "${height?.replace("cm", "")}"
    height_tv.text = height
}

override fun onNegativeButtonClicked() {

}

覆盖正面按钮点击(列表:数组?,位置:Int){
val高度=列表?获取(位置)
高度位置=位置
newHeight=“${height?.replace”(“cm”,”)}
高度\u tv.text=高度
}
覆盖onNegativeButtonClicked(){
}

设置SingleChoiceItems中的第二个参数负责对话框中的默认值。
如果要显示此对话框中最后选定的项目,应将其保存在该位置的某个位置,稍后调用此对话框时,请返回并设置值。

设置SingleChoiceItems中的第二个参数负责对话框中的默认值。
如果要显示此对话框中最后选定的项目,应将其保存在该位置的某个位置,稍后调用此对话框时,请返回值并进行设置。

您可以在父活动中保存最后选定的位置

其他选项您可以在首选项中保存它

做一个Pref类

 object Pref {
   private val LastSelecetedPosition = "last_selection"

   fun getLastSelection(context: Context): Int {
            return getPrefs(context).getInt(LastSelecetedPosition,0)
        }

  fun setLastSelection(context: Context, lastSelection: Int) {
    getPrefs(context).edit().putInt(LastSelecetedPosition,  lastSelection).apply()
   }
}
现在,当您在pref中设置值之后保存选择时

pref.setLastSelection(context,4)  //set your option at here
最后,当你们想要取回你们最后的选择,而不仅仅是使用下面的行

pref.getLastSelection(context)

希望这将对您有所帮助。

您可以在家长活动中保存上次选择的职位

其他选项您可以在首选项中保存它

做一个Pref类

 object Pref {
   private val LastSelecetedPosition = "last_selection"

   fun getLastSelection(context: Context): Int {
            return getPrefs(context).getInt(LastSelecetedPosition,0)
        }

  fun setLastSelection(context: Context, lastSelection: Int) {
    getPrefs(context).edit().putInt(LastSelecetedPosition,  lastSelection).apply()
   }
}
现在,当您在pref中设置值之后保存选择时

pref.setLastSelection(context,4)  //set your option at here
最后,当你们想要取回你们最后的选择,而不仅仅是使用下面的行

pref.getLastSelection(context)
希望这对你有帮助