保存数组中的最后一个字符串并再次打开应用程序后显示

时间:2015-07-04 21:16:27

标签: android sharedpreferences

这是我的代码,当我的应用程序再次打开时,我需要在屏幕上显示最后一个字符串和字符串编号。谢谢你的帮助。

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;
    }

}
}

2 个答案:

答案 0 :(得分:0)

好的,所以你设置了共享首选项编辑器,你现在需要做的是:

  • 在您再次打开屏幕时保存您想要的任何共享偏好设置
  • 通过覆盖活动的onResume()方法或者您想要加载它的任何地方,从共享首选项加载内容(在您的情况下为字符串)

仅作为保存的一个例子:

    // 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;
    }

}

}