【问题标题】:Saving last string from array and show it after open app again从数组中保存最后一个字符串并在再次打开应用程序后显示它
【发布时间】:2015-07-04 21:16:26
【问题描述】:

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

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

}
}

【问题讨论】:

  • 在请其他人编写代码之前,自己尝试问题或多阅读一些内容。尝试使用MCVE 重新表述问题以结构化
  • 您创建了一个 sharedPreference 但没有使用它。我建议将值保存在 onPause 中,然后在 onCreate 中再次加载
  • 对不起,我没有提到我正在尝试使用 sharedPreferences 并删除了不起作用的代码,因为我说我是编程新手,如果有人可以告诉我如何在这种情况下使用共享首选项,它会很棒
  • 您可以使用 BranMoney 答案作为参考,然后可以检查我在答案中所做的更改,我也应该建议在 developer.android.com/reference/android/content/… 正确阅读

标签: android sharedpreferences


【解决方案1】:

在这里用这个替换你的代码,它应该可以完成这项工作

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

}

}

【讨论】:

  • 谢谢它的工作。我真的花了很多时间来寻找如何在互联网上使用共享首选项,并且只有教程如何使用来保存和加载用户名和密码。我什至不知道我可以在暂停时使用它。无论如何,我还有很多东西要学,再次感谢,祝你好运。
【解决方案2】:

好的,您已经设置了共享首选项编辑器,您现在需要做的是:

  • 保存您想要共享的首选项以在您再次打开屏幕时加载
  • 通过覆盖活动的 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");

希望这会有所帮助!

【讨论】:

  • 感谢您的回答,我也会尝试检查它是如何工作的。
  • 一切都好,正如 Maverick 所说 - 我的回答更像是一个参考,您可以在必要时在其他项目中使用。
  • 谢谢你们,这将帮助我理解 sharedPreferences。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多