即使我从应用程序退出,我希望应用程序存储数据。这就是我希望我的应用程序如何工作的方式。假设有3个活动,Main,A,B和C.主要用于显示,A是编辑值的菜单,B和C是编辑界面。在转到B进行编辑之后,它将返回到A,如果我去C编辑其他内容,我的B数据将不会进行任何更改。但我无法做到,我面临的问题是它总是自动删除其他数据。请帮助,谢谢。
主
package com.example.suntracking;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener{
TextView aziangle,elevation,numberof,rightasc,decli,hourangle,solartime,showinlat,showinlong;
Button editting;
Long latval,longval;
String show1,show2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
loadSavedpreferences();
}
private void loadSavedpreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata=PreferenceManager.getDefaultSharedPreferences(this);
show1=savedata.getString("show1",null);
latval=savedata.getLong("latitudevalue", 0);
showinlat.setText("Latitude is : " + latval + ", " + show1);
}
private void initialize() {
// TODO Auto-generated method stub
aziangle=(TextView)findViewById(R.id.tvAziAngle);
elevation=(TextView)findViewById(R.id.tvElevation);
numberof=(TextView)findViewById(R.id.tvNumberof);
rightasc=(TextView)findViewById(R.id.tvRightAsc);
decli=(TextView)findViewById(R.id.tvDecli);
hourangle=(TextView)findViewById(R.id.tvHourAngle);
solartime=(TextView)findViewById(R.id.tvSolarTime);
showinlat=(TextView)findViewById(R.id.tvShowInLat);
showinlong=(TextView)findViewById(R.id.tvShowInLong);
editting=(Button)findViewById(R.id.bedit);
editting.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent e=new Intent(MainActivity.this, Parameterlist.class);
savePreferences("show1",show1);
savePreferences("latval",latval);
startActivityForResult(e,0);
}
private void savePreferences(String string, Long latval2) {
// TODO Auto-generated method stub
SharedPreferences savedata = getApplicationContext().getSharedPreferences(
"savealldata", MODE_PRIVATE);
Editor editor=savedata.edit();
editor.putLong("latval", latval);
editor.commit();
}
private void savePreferences(String string, String show1) {
// TODO Auto-generated method stub
SharedPreferences savedata = getApplicationContext().getSharedPreferences(
"savealldata", MODE_PRIVATE);
Editor editor=savedata.edit();
editor.putString("show1", show1);
editor.commit();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
loadSavedpreferences();
Bundle getanswer=data.getExtras();
show1=getanswer.getString("showloc1");
latval=getanswer.getLong("anslatitudevalue");
showinlat.setText("Latitude is : " + latval + ", " + show1);
show2=getanswer.getString("longiloc");
longval=getanswer.getLong("anslongitudevalue");
showinlong.setText("Latitude is : " + longval + ", " + show2);
savePreferences("show1",show1);
savePreferences("latval",latval);
}
}
} 菜单
package com.example.suntracking;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Parameterlist extends ListActivity{
String selection[]={"Latitude","Longitude","TimeZone","DaylightSavingTime","OffsetParameters"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Parameterlist.this, android.R.layout.simple_list_item_1, selection));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String choosen= selection[position];
try{
Class ourclass = Class.forName("com.example.suntracking." + choosen);
Intent ourintent=new Intent(Parameterlist.this,ourclass);
startActivityForResult(ourintent,0);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK){
Bundle ans=data.getExtras();
Intent ansall=new Intent();
ansall.putExtras(ans);
setResult(RESULT_OK,ansall);
finish();
}
}
}
活动B
package com.example.suntracking;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Latitude extends Activity implements OnClickListener,OnCheckedChangeListener{
EditText latitudein;
Button savelatitude;
RadioGroup nschoose;
String latitudeloc,temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.latitude);
initialize();
}
private void initialize() {
// TODO Auto-generated method stub
latitudein=(EditText)findViewById(R.id.etLatitude);
savelatitude=(Button)findViewById(R.id.bSaveLatitude);
nschoose=(RadioGroup)findViewById(R.id.rgChoicelatitude);
savelatitude.setOnClickListener(this);
nschoose.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch(arg1){
case R.id.rbNorth:
latitudeloc="North";
break;
case R.id.rbSouth:
latitudeloc="South";
break;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
temp = latitudein.getText().toString();
try{
Intent backtomain1=new Intent();
Bundle answerlat=new Bundle();
answerlat.putString("showloc1", latitudeloc);
Long intlatitudevalue=Long.parseLong(temp);
answerlat.putLong("anslatitudevalue", intlatitudevalue);
backtomain1.putExtras(answerlat);
setResult(RESULT_OK,backtomain1);
finish();
}catch (NumberFormatException e){
e.printStackTrace();
}
}
}
活动C
package com.example.suntracking;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Longitude extends Activity implements OnClickListener,OnCheckedChangeListener{
EditText longitudein;
RadioGroup longitudechoice;
Button savelongitude;
String longitudeloc,temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.longitude);
initialize();
}
private void initialize() {
// TODO Auto-generated method stub
longitudein=(EditText)findViewById(R.id.etLongitude);
longitudechoice=(RadioGroup)findViewById(R.id.rgChoicelongitude);
savelongitude=(Button)findViewById(R.id.bSaveLongitude);
savelongitude.setOnClickListener(this);
longitudechoice.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch(checkedId){
case R.id.rbEast:
longitudeloc="East";
break;
case R.id.rbWest:
longitudeloc="West";
break;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
temp = longitudein.getText().toString();
try{
Bundle anslong=new Bundle();
anslong.putString("longiloc", longitudeloc);
Long intlongitudevalue=Long.parseLong(temp);
anslong.putLong("anslongitudevalue", intlongitudevalue);
Intent backtomain2=new Intent();
backtomain2.putExtras(anslong);
setResult(RESULT_OK,backtomain2);
finish();
}catch (NumberFormatException e){
e.printStackTrace();
}
}
}
我尝试通过使用SharedPreferences使其变得简单。这是代码。现在我有另一个问题是它保存了初始数据,但即使我输入新数据也不会做任何更改。所以我认为我没有在mainactivity中取出它,我该怎么做才能将它从SharedPreferences中取出来。
主要
package com.example.suntracking;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener{
TextView aziangle,elevation,numberof,rightasc,decli,hourangle,solartime,showinlat,showinlong;
Button editting;
Long latval,longval;
String show1,show2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
loadSavedpreferences();
}
private void loadSavedpreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata= getSharedPreferences("savealldata",0);
show1=savedata.getString("show1",null);
latval=savedata.getLong("latval", 0);
showinlat.setText("Latitude is : " + latval + ", " + show1);
}
private void initialize() {
// TODO Auto-generated method stub
aziangle=(TextView)findViewById(R.id.tvAziAngle);
elevation=(TextView)findViewById(R.id.tvElevation);
numberof=(TextView)findViewById(R.id.tvNumberof);
rightasc=(TextView)findViewById(R.id.tvRightAsc);
decli=(TextView)findViewById(R.id.tvDecli);
hourangle=(TextView)findViewById(R.id.tvHourAngle);
solartime=(TextView)findViewById(R.id.tvSolarTime);
showinlat=(TextView)findViewById(R.id.tvShowInLat);
showinlong=(TextView)findViewById(R.id.tvShowInLong);
editting=(Button)findViewById(R.id.bedit);
editting.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savePreferences();
Intent e=new Intent(MainActivity.this, Parameterlist.class);
savePreferences();
startActivity(e);
}
private void savePreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata = getSharedPreferences("savealldata",0);
Editor editor=savedata.edit();
editor.putLong("latval", latval);
editor.putString("show1", show1);
editor.commit();
}
菜单
package com.example.suntracking;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Parameterlist extends ListActivity{
String selection[]={"Latitude","Longitude","TimeZone","DaylightSavingTime","OffsetParameters"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Parameterlist.this, android.R.layout.simple_list_item_1, selection));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String choosen= selection[position];
try{
Class ourclass = Class.forName("com.example.suntracking." + choosen);
Intent ourintent=new Intent(Parameterlist.this,ourclass);
startActivity(ourintent);
finish();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
ACT B
package com.example.suntracking;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class Latitude extends Activity implements OnClickListener,OnCheckedChangeListener{
EditText latitudein;
Button savelatitude;
RadioGroup nschoose;
String latitudeloc,temp;
Long intlatitudevalue;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.latitude);
initialize();
}
private void initialize() {
// TODO Auto-generated method stub
latitudein=(EditText)findViewById(R.id.etLatitude);
savelatitude=(Button)findViewById(R.id.bSaveLatitude);
nschoose=(RadioGroup)findViewById(R.id.rgChoicelatitude);
savelatitude.setOnClickListener(this);
nschoose.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch(arg1){
case R.id.rbNorth:
latitudeloc="North";
break;
case R.id.rbSouth:
latitudeloc="South";
break;
}
}
@Override
public void onClick(View v) {
temp = latitudein.getText().toString();
try{
intlatitudevalue=Long.parseLong(temp);
savePreferences();
finish();
}catch (NumberFormatException e){
e.printStackTrace();
}
}
private void savePreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata = getSharedPreferences("savealldata",0);
Editor editor=savedata.edit();
editor.putLong("latval", intlatitudevalue);
editor.putString("show1", latitudeloc);
editor.commit();
}
编辑:我通过强制重启应用程序找到了解决方案。任何其他方法??
答案 0 :(得分:0)
问题在于PreferenceManager.getDefaultSharedPreferences(...)
的召唤。
特别是,您将通过调用检索SharedPreferences对象,该对象未命名为“savealldata”,但名为[$YourPackageName]_preferences
。
因此,您应该使用相同的名称确保SharedPreferences。
参见:$ android-sdk \ sources \ android-18 \ android \ preference \ PreferenceManager.java