我在此网站上的某个人告诉我使用它来存储文本字段的值后使用共享首选项。但是,在我实现它之后,我仍然无法在EditText字段中存储数据。
我的主要java活动(代码的相关位)是:
package com.example.fahadsaleem.xyz;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.SharedPreferences;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
public String str = " ";
public static final String sub_name = "Subject Key: ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView calc_monday = (TextView) findViewById(R.id.monday_calc);
final SharedPreferences sharedPreferences = getSharedPreferences("AllData", Context.MODE_PRIVATE);
calc_monday.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.show();
TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);
text1.setText(str);
TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
text2.setText("6 (SEECS)");
TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
text3.setText("09:00am 09:50am");
}
}
);
calc_monday.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
kj monday_calc = new kj(MainActivity.this);
monday_calc.show();
EditText set_monday_calc = (EditText) monday_calc.findViewById(R.id.set_Subject_ID);
str = set_monday_calc.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(sub_name, str);
editor.commit();
return true;
}
}
);
基本上,我的应用程序有一个时间表界面。时间表包含不同时间的各种科目的名称。当我单击(点击)主题名称时,会出现一个对话框,显示主题名称,时间和房间号码。这个主题。
我希望能够改变我想要的这3件事。所以我创建了另一个对话框,应该会长按一下。在此对话框中输入的文本(现在只是主题名称)应存储在字符串str
中,然后只需单击一次即可显示此str
。
但是这段代码不起作用。
当我长按一下这样输入微积分时,会显示以下屏幕:
但是当我点击返回然后再次单击主题名称时,没有显示任何内容!
问题是什么?我是否以错误的方式实施共享偏好?
编辑:我添加了一些用户建议的存储代码,但代码仍无效。仍未将微积分存储在场上。单击时,EditText字段仍为空白。这是我编辑后的新的主要活动onLongClickListener代码:
EDIT-2:我在onClickListener函数中更改了代码,它现在看起来像这样:
但即使是现在结果也一样。此外,当我在设置名称的对话框中键入微积分后单击返回时显示以下错误:
答案 0 :(得分:0)
首先,将sub_name
值更改为"subjectKey"
。使用驼峰式格式。
接下来,您要存储价值:
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(sub_name, str);
editor.commit();
但是没有代码可以检索已保存的值。
访问此页面:How to use SharedPreferences in Android to store, fetch and edit values
编辑:此部分负责检索已保存的数据:
String text = sharedPreferences.getString(sub_name, "");
if(text != ""){ //you need to check if value is empty
set_monday_calc.setText(text); //otherwise, you may get NPE
}
str = set_monday_calc.getText().toString(); //now you can use `str` variable
//it has now saved value
希望有所帮助
答案 1 :(得分:0)
如果要将值存储在SharedPreferences
中,那么为什么要使用全局变量str
。只是为了一个建议,在View#OnClickListener
获取密钥SUBJECT_ID
的值并将其显示在TextView
答案 2 :(得分:0)
您只存储数据。您必须检索它并将文本加载到EditText中,如下所示,
String text = sharedPreferences.getString(sub_name, "");
if(text != ""){
set_monday_calc.setText(text);
}
编辑:如果我理解正确,您必须设置ID为R.id.Subject_ID
的TextView的值。尝试在onClickListener()
,
calc_monday.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.show();
TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);
String text = sharedPreferences.getString(sub_name, "");
if(text != ""){
text1.setText(text);
}
TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
text2.setText("6 (SEECS)");
TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
text3.setText("09:00am 09:50am");
}
}
);
答案 3 :(得分:0)
您必须使用共享首选项检索您存储的数据:
String value = sharedPreferences.getString(sub_name, null);
否则一切看起来都不错......除了代码的格式化(使用camel-case)