在textview的运行时更改了文本,但是在重新启动应用程序后在android的textview中检索了默认数据 [英] Changed text at the runtime in textview but after relaunching application retrieving default data in textview in android

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

问题描述

我使用共享首选项存储来自 EditText 的数据,并将首选项数据设置回 TextView ,但是当我重新打开应用程序时,textview显示默认值.如何将更改的数据设置为 TextView ,并且在重新打开应用程序后数据不应丢失.我尝试了 onSaveInstanceState() onSaveInstanceState(),但这在应用程序方向更改时有效.

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.

在我的代码中,我将数据存储到共享的首选项"中,并将该数据返回到 TextView PRESET_MESSAGE_ONE中,我正在存储 EditText 的值.

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(){

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();

        }
    });

并在onResume方法()中读取SharePreferences字符串

and read SharePreferences string in onResume method()

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

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