这是我的代码,当我的应用程序再次打开时,我需要在屏幕上显示最后一个字符串和字符串编号。谢谢你的帮助。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String [] tabela;
TextView textView;
TextView textView2;
TextView textView3;
Button next, prev;
int index;
SharedPreferences sPref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sPref = getSharedPreferences("daniel.myapplication", Context.MODE_PRIVATE);
editor = sPref.edit();
// Importing the string array from Valuses folder
tabela = getResources().getStringArray(R.array.array);
// initialization of textview
textView =(TextView)findViewById(R.id.textView);
textView2 =(TextView)findViewById(R.id.textView2);
textView3 =(TextView)findViewById(R.id.textView3);
//Initialization of buttons
next =(Button)findViewById(R.id.button);
prev =(Button)findViewById(R.id.button2);
//OnClickListener for buttons
next.setOnClickListener(this);
prev.setOnClickListener(this);
// Setting values for our variable and textviews
index = 0;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
textView3.setText("/"+String.valueOf(tabela.length));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button2:
index++;
if (index==tabela.length){
index=0;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
}else {
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index + 1));
}
break;
case R.id.button:
index--;
if (index ==-1){
index = tabela.length -1;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
}else {
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index + 1));
}
break;
}
}
}
答案 0 :(得分:0)
好的,所以你设置了共享首选项编辑器,你现在需要做的是:
仅作为保存的一个例子:
// Get shared preferences of the application context
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
// Get the editor from the shared preferences
SharedPreferences.Editor editor = settings.edit();
// Do what you want here
editor.putString("string key", "the string you want to save");
// Commit the changes to the preferences
editor.commit();
然后当你想加载东西时(我想是onResume()方法)
// Get shared preferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
// Get your string from shared preferences
String yourString = settings.getString("string key", "default string");
希望这有帮助!
答案 1 :(得分:0)
这里用这个替换你的代码,它应该完成这项工作
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String [] tabela;
TextView textView;
TextView textView2;
TextView textView3;
Button next, prev;
int index;
SharedPreferences sPref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sPref = getSharedPreferences("daniel.myapplication", Context.MODE_PRIVATE);
editor = sPref.edit();
// Importing the string array from Valuses folder
tabela = getResources().getStringArray(R.array.array);
// initialization of textview
textView =(TextView)findViewById(R.id.textView);
textView2 =(TextView)findViewById(R.id.textView2);
textView3 =(TextView)findViewById(R.id.textView3);
//Initialization of buttons
next =(Button)findViewById(R.id.button);
prev =(Button)findViewById(R.id.button2);
//OnClickListener for buttons
next.setOnClickListener(this);
prev.setOnClickListener(this);
// Setting values for our variable and textviews
index = sPref.getInt("key", 0);
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
textView3.setText("/"+String.valueOf(tabela.length));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPause(){
super.onPause();
editor.putInt("key",index);
editor.commit();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button2:
index++;
if (index==tabela.length){
index=0;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
}else {
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index + 1));
}
break;
case R.id.button:
index--;
if (index ==-1){
index = tabela.length -1;
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index+1));
}else {
textView.setText(tabela[index]);
textView2.setText(String.valueOf(index + 1));
}
break;
}
}
}