我正在尝试使用onsaveinstancestate在我的Edittext视图中保留数据。用户点击"添加"按钮,它应该保留Edittext视图中的信息。单击添加按钮后,用户将返回活动主页。选择模块进行编辑时,会显示edit_module布局,但没有edittext视图中的信息,我试图保留这些信息。有任何想法吗??我想我可能会错过一个步骤,除了在onsaveinstancestate方法中保存字符串,然后在调用活动时将这些字符串分配给Edittext视图? Android新手。
NewModule.java
public class NewModule extends Activity{
// The EditText objects
EditText ModuleCode;
EditText ModuleName;
EditText ModuleType;
EditText DayOfWeek;
EditText StartTime;
EditText EndTime;
EditText Location;
EditText AdditionalInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
// Get saved data if there is any
super.onCreate(savedInstanceState);
dbTools = new DBTools(this);
// Designate that add_module.xml is the interface used
setContentView(R.layout.add_module);
// Initialize the EditText objects
ModuleCode= (EditText) findViewById(R.id.modcodeet);
ModuleName = (EditText) findViewById(R.id.modnameet);
ModuleType = (EditText) findViewById(R.id.moduletypeet);
DayOfWeek = (EditText) findViewById(R.id.dowet);
StartTime = (EditText) findViewById(R.id.starttimeet);
EndTime = (EditText) findViewById(R.id.endtimeet);
Location = (EditText) findViewById(R.id.locationet);
AdditionalInfo = (EditText) findViewById(R.id.additionalinfoet);
}
public void addNewModule(View view) {
// Will hold the HashMap of values
HashMap<String, String> queryValuesMap = new HashMap<String, String>();
// Get the values from the EditText boxes
queryValuesMap.put("ModuleCode", ModuleCode.getText().toString());
queryValuesMap.put("ModuleName", ModuleName.getText().toString());
queryValuesMap.put("ModuleType", ModuleType.getText().toString());
queryValuesMap.put("DayOfWeek", DayOfWeek.getText().toString());
queryValuesMap.put("StartTime", StartTime.getText().toString());
queryValuesMap.put("EndTime", EndTime.getText().toString());
queryValuesMap.put("Location", Location.getText().toString());
queryValuesMap.put("AdditionalInfo", AdditionalInfo.getText().toString());
// Call for the HashMap to be added to the database
dbTools.insertModule(queryValuesMap);
// Call for MainActivity to execute
this.callMainActivity(view);
}
public void callMainActivity(View view) {
Intent theIntent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(theIntent);
}
public void onSaveInstanceState(Bundle savedInstanceState){
EditText editText = (EditText) findViewById(R.id.modcodeet);
String code = editText.getText().toString();
savedInstanceState.putString("code", code);
EditText editText2 = (EditText) findViewById(R.id.modnameet);
String name = editText2.getText().toString();
savedInstanceState.putString("name", name);
EditText editText3 = (EditText) findViewById(R.id.moduletypeet);
String type = editText3.getText().toString();
savedInstanceState.putString("type", type);
EditText editText4 = (EditText) findViewById(R.id.dowet);
String dow = editText4.getText().toString();
savedInstanceState.putString("dow", dow);
EditText editText5 = (EditText) findViewById(R.id.starttimeet);
String messagesubject = editText5.getText().toString();
savedInstanceState.putString("start", messagesubject);
EditText editText6 = (EditText) findViewById(R.id.endtimeet);
String end = editText6.getText().toString();
savedInstanceState.putString("end",end);
EditText editText7 = (EditText) findViewById(R.id.locationet);
String location = editText7.getText().toString();
savedInstanceState.putString("location", location);
EditText editText8 = (EditText) findViewById(R.id.additionalinfoet);
String additionalinfo = editText8.getText().toString();
savedInstanceState.putString("additionalinfo", additionalinfo);
super.onSaveInstanceState(savedInstanceState);
}
}
TIA
编辑Module.java
public class EditModule extends Activity{
EditText ModuleCode;
EditText ModuleName;
EditText ModuleType;
EditText DayOfWeek;
EditText StartTime;
EditText EndTime;
EditText Location;
EditText AdditionalInfo;
DBTools dbTools = new DBTools(this);
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_module);
if (savedInstanceState != null)
{
String strValue = savedInstanceState.getString("code");
if (strValue != null);
ModuleCode = (EditText)findViewById(R.id.modcodeet);
ModuleCode.setText(strValue);
strValue = savedInstanceState.getString("name");
if (strValue != null);
ModuleName = (EditText)findViewById(R.id.modnameet);
ModuleName.setText(strValue);
strValue = savedInstanceState.getString("type");
if(strValue != null);
ModuleType = (EditText)findViewById(R.id.moduletypeet);
ModuleType.setText(strValue);
strValue = savedInstanceState.getString("dow");
if(strValue != null);
DayOfWeek = (EditText)findViewById(R.id.dowet);
DayOfWeek.setText(strValue);
strValue = savedInstanceState.getString("start");
if (strValue != null);
StartTime = (EditText)findViewById(R.id.starttimeet);
StartTime.setText(strValue);
strValue = savedInstanceState.getString("end");
if (strValue != null);
EndTime = (EditText)findViewById(R.id.endtimeet);
EndTime.setText(strValue);
strValue = savedInstanceState.getString("location");
if (strValue != null);
Location = (EditText)findViewById(R.id.locationet);
Location.setText(strValue);
strValue = savedInstanceState.getString("additionalinfo");
if (strValue != null);
AdditionalInfo = (EditText)findViewById(R.id.additionalinfoet);
AdditionalInfo.setText(strValue);
}
public void editModule(View view){
HashMap<String, String> queryValuesMap = new HashMap<String, String>();
ModuleName = (EditText) findViewById(R.id.modnameet);
ModuleType = (EditText) findViewById(R.id.moduletypeet);
DayOfWeek = (EditText) findViewById(R.id.dowet);
StartTime = (EditText) findViewById(R.id.starttimeet);
EndTime = (EditText) findViewById(R.id.endtimeet);
Location = (EditText) findViewById(R.id.locationet);
AdditionalInfo = (EditText) findViewById(R.id.additionalinfoet);
this.callMainActivity(view);
}
public void callMainActivity(View view){
Intent objIntent = new Intent(getApplication(), MainActivity.class);
startActivity(objIntent);
}
}
答案 0 :(得分:1)
使用SharedPreferences保存edittext值
答案 1 :(得分:1)
尝试这样:
//intialization of shared preferences
private SharedPreferences preferences;
//in oncreate() give like this:
preferences = PreferenceManager.getDefaultSharedPreferences(this);
// store the edittext value in shared preferences
Editor edit = preferences.edit();
edit.putString("edittextvalue", edittextvalue);
edit.commit();
//whereever u want to get value and use
String apptext = preferences.getString("edittextvalue","");