在TextView中运行,但在的Andr​​iod TextView的重振应用程序检索默认数据后更改文本 [英] Changed text at the runtime in textview but after relaunching application retrieving default data in textview in andriod

查看:154
本文介绍了在TextView中运行,但在的Andr​​iod TextView的重振应用程序检索默认数据后更改文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的共享preferences存储是来自的EditText 数据集preferences数据传回的TextView 但是当我重新打开我的应用程序TextView中显示的默认值。如何设置更改的数据在的TextView 和数据不应该重启程序后丢失。我试图的onSaveInstanceState()的onSaveInstanceState(),但这个工程时,应用程序的方向改变。

在这里,在我的codeI数据存储到共享preferences和获取数据回<$的的TextView preSET_MESSAGE_ONE我正在存储值C $ C>的EditText 。

 公共无效customDialogOne(){
    支持mDialog =新的对话框(_con);
    mDialog.setContentView(R.layout.custom_dialog_message);
    。mDialog.getWindow()setBackgroundDrawableResource(R.color.black);
    mDialog.setTitle(编辑preSET消息);    BTN presetDialogCancel =(按钮)支持mDialog
            .findViewById(R.id.btnPrDialogCancel);
    EDT presetDialogMessage =(EditText上)支持mDialog
            .findViewById(R.id.edtPrDialogMessage);    BTN presetDialogSave =(按钮)支持mDialog
            .findViewById(R.id.btnPrDialogSave);
    BTN presetDialogSave.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            共享pref.writeString(共享preF。preSET_MESSGE_ONE,
                    EDT presetDialogMessage.getText()的toString());
            msgOne =共享pref.readString(共享preF preSET_MESSGE_ONE);
            tm.showToast(msgOne);
            tvFr presetMsgOne.setText(msgOne);
            mDialog.dismiss();        }
    });
    BTN presetDialogCancel.setOnClickListener(新View.OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            mDialog.dismiss();
        }
    });
    mDialog.show();
}


解决方案

我的问题解决了我的顺序是错误的共享preferences存储值,我用下面的code:

BTN presetDialogSave.setOnClickListener(新View.OnClickListener(){

  @覆盖
        公共无效的onClick(视图v){            tvFr presetMsgOne.setText(EDT presetDialogMessage.getText()
                    的ToString());
            共享pref.writeString(共享preF。preSET_MESSGE_ONE,
                    EDT presetDialogMessage.getText()的toString());            msgOne =共享pref.readString(共享preF preSET_MESSGE_ONE);
            tvFr presetMsgOne.setText(msgOne);
            mDialog.dismiss();        }
    });

和阅读onResume方法分享preferences字符串()

I am using Shared Preferences to store data which is came from EditText and set preferences data back to TextView but when i reopen my application textview shows default value. How can set changed data to the TextView and data should not be lost after reopening application. I tried onSaveInstanceState() and onSaveInstanceState() but this works when orientation change of application.

Here in my code i store data into shared Preferences and getting that data back to the TextView PRESET_MESSAGE_ONE i am storing value of EditText.

public void customDialogOne() {
    mDialog = new Dialog(_con);
    mDialog.setContentView(R.layout.custom_dialog_message);
    mDialog.getWindow().setBackgroundDrawableResource(R.color.black);
    mDialog.setTitle("Edit Preset Message");

    btnPresetDialogCancel = (Button) mDialog
            .findViewById(R.id.btnPrDialogCancel);
    edtPresetDialogMessage = (EditText) mDialog
            .findViewById(R.id.edtPrDialogMessage);

    btnPresetDialogSave = (Button) mDialog
            .findViewById(R.id.btnPrDialogSave);
    btnPresetDialogSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            SharedPref.writeString(SharedPref.PRESET_MESSGE_ONE,
                    edtPresetDialogMessage.getText().toString());
            msgOne = SharedPref.readString(SharedPref.PRESET_MESSGE_ONE);
            tm.showToast(msgOne);
            tvFrPresetMsgOne.setText(msgOne);
            mDialog.dismiss();

        }
    });
    btnPresetDialogCancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mDialog.dismiss();
        }
    });
    mDialog.show();
}

解决方案

My issue has resolved my sequence was wrong to store value in shared Preferences, I used following code:

btnPresetDialogSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            tvFrPresetMsgOne.setText(edtPresetDialogMessage.getText()
                    .toString());
            SharedPref.writeString(SharedPref.PRESET_MESSGE_ONE,
                    edtPresetDialogMessage.getText().toString());

            msgOne = SharedPref.readString(SharedPref.PRESET_MESSGE_ONE);
            tvFrPresetMsgOne.setText(msgOne);
            mDialog.dismiss();

        }
    });

and read SharePreferences string in onResume method()

这篇关于在TextView中运行,但在的Andr​​iod TextView的重振应用程序检索默认数据后更改文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆