简体   繁体   中英

How can I make my android app have SharedPreferences as just one set of preferences, so that I can control it and clear the preferences with a button

I am trying to have SharedPreferences once and control it to different methods rather than having different SharedPreferences in different methods. I have SharedPreferences in the onCreate, LoadPreferences, SavePreferences and ClearTextViews methods. I want to make it so that I have preferences saved to the TextViews from entered text in the EditText and then be able to clear them all with a button. Please help me if you can. Here is the relevant code:

public class notesActivity extends Activity implements OnClickListener {

Button saveNote;
Button clearText;
EditText note;
TextView textSavedNote1, textSavedNote2, textSavedNote3, textSavedNote4, textSavedNote5, textSavedNote6;
SharedPreferences spNote;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notes);

    saveNote = (Button)this.findViewById(R.id.saveNotes);
    saveNote.setOnClickListener(this);

    clearText = (Button)this.findViewById(R.id.clearAllText);
    clearText.setOnClickListener(this);

    textSavedNote1 = (TextView)findViewById(R.id.stringSavedNote1);
    textSavedNote2 = (TextView)findViewById(R.id.stringSavedNote2);
    textSavedNote3 = (TextView)findViewById(R.id.stringSavedNote3);
    textSavedNote4 = (TextView)findViewById(R.id.stringSavedNote4);
    textSavedNote5 = (TextView)findViewById(R.id.stringSavedNote5);
    textSavedNote6 = (TextView)findViewById(R.id.stringSavedNote6);

    note = (EditText)this.findViewById(R.id.notes);

    spNote = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor edit = spNote.edit();
    edit.putString("note"+saveNote,note.getText().toString());
    edit.commit();
}



public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Goes back to the home screen
    case R.id.item1:  Intent i = new Intent(notesActivity.this, UserSettingActivity.class);
    startActivity(i);

    case R.id.item2:
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
    // Refreshes the page.   
    case R.id.item3:
            finish();
            startActivity(getIntent());
        default:
            return super.onOptionsItemSelected(item);
    }
}


public void onClick (View v){
    if(v==saveNote){
        SavePreferences("NOTE1", note.getText().toString());
        LoadPreferences();
        note.setText("");
        if(textSavedNote1.getText().toString().length()>0){
            SavePreferences("NOTE2", note.getText().toString());
            LoadPreferences();
            note.setText("");
        }
        else{
    }
}
    else if(v==clearText){
        ClearTextViews();
    }
}

private void SavePreferences(String key, String value){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
   }

private void LoadPreferences(){
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    String strSavedNote1 = sharedPreferences.getString("NOTE1", "");
    String strSavedNote2 = sharedPreferences.getString("NOTE2", "");
    String strSavedNote3 = sharedPreferences.getString("NOTE3", "");
    String strSavedNote4 = sharedPreferences.getString("NOTE4", "");
    String strSavedNote5 = sharedPreferences.getString("NOTE5", "");
    String strSavedNote6 = sharedPreferences.getString("NOTE6", "");
    textSavedNote1.setText(strSavedNote1);
    textSavedNote2.setText(strSavedNote2);
    textSavedNote3.setText(strSavedNote3);
    textSavedNote4.setText(strSavedNote4);
    textSavedNote5.setText(strSavedNote5);
    textSavedNote6.setText(strSavedNote6);
   }

private void ClearTextViews(){ 

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.commit();

}

}

Try the code below.

/*I have added one more button, on click on that button i have set call clearText function and set all text     empty, each time before clicking on save button you click first clear button then next value will be inserted in next textview.*/
public class SharedPreferenceJustOneSetOfPreferencesActivity extends Activity implements OnClickListener{

    private Button saveNote,clearText,ClearAll;
    static TextView textSavedNote1, textSavedNote2, textSavedNote3, textSavedNote4, textSavedNote5, textSavedNote6;
    private EditText note;
    private SharedPreferences spNote;
    private static final String TAG = SharedPreferenceJustOneSetOfPreferencesActivity.class.getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
        saveInPreference();
    }

    private void initView() {
        textSavedNote1 = (TextView)findViewById(R.id.textSavedNote1);
        textSavedNote2 = (TextView)findViewById(R.id.textSavedNote2);
        textSavedNote3 = (TextView)findViewById(R.id.textSavedNote3);
        textSavedNote4 = (TextView)findViewById(R.id.textSavedNote4);
        textSavedNote5 = (TextView)findViewById(R.id.textSavedNote5);
        textSavedNote6 = (TextView)findViewById(R.id.textSavedNote6);
        note = (EditText)findViewById(R.id.note);
        saveNote = (Button)findViewById(R.id.saveNote);
        clearText = (Button)findViewById(R.id.clearText);
        ClearAll = (Button)findViewById(R.id.ClearAll);
        saveNote.setOnClickListener(this);
        clearText.setOnClickListener(this); 
        ClearAll.setOnClickListener(this);
    }
    private void saveInPreference() {
        spNote = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor edit = spNote.edit();
        edit.putString("note"+saveNote,note.getText().toString());
        edit.commit();
    }
    public boolean onCreateOptionsMenu(Menu menu){
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        return true;        
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch (item.getItemId()) {
        case R.id.item1:  Intent i = new Intent(SharedPreferenceJustOneSetOfPreferencesActivity.this, UserSettingActivity.class);
        startActivity(i);
        case R.id.item2:
                Intent intent = new Intent(SharedPreferenceJustOneSetOfPreferencesActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
        case R.id.item3:
              finish();
              startActivity(getIntent());
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    @Override
    public void onClick(View view) {
        if (view == saveNote) {
            String textvedNote1 =textSavedNote1.getText().toString();
            String textvedNote2= textSavedNote2.getText().toString();
            String textvedNote3 =textSavedNote3.getText().toString();
            String textvedNote4= textSavedNote4.getText().toString();
            String textvedNote5 =textSavedNote5.getText().toString();
            String textvedNote6= textSavedNote6.getText().toString();

            if (textvedNote1.equals("")) {          
                SavePreferences("NOTE1", note.getText().toString());
                Log.e(TAG,"textvedNote1: Inside "+textvedNote1.length());
                LoadPreferences();
                note.setText("");
            }else  if (textvedNote2.equals("")&& !textvedNote1.equals("")) {            
                SavePreferences("NOTE2", note.getText().toString());
                Log.e(TAG,"textvedNote2: Inside "+textvedNote1.length());
                LoadPreferences();
                note.setText("");
            } else if (textvedNote3.equals("")&& !textvedNote2.equals("")) {
                Log.e(TAG,"textvedNote3: Inside "+textvedNote2.length());
                SavePreferences("NOTE3", note.getText().toString());
                LoadPreferences();
                note.setText("");
            } else if (textvedNote4.equals("")&& !textvedNote3.equals("")) {
                Log.e(TAG,"textvedNote4: Inside "+textvedNote3.length());
                SavePreferences("NOTE4", note.getText().toString());
                LoadPreferences();
                note.setText("");
            } else if (textvedNote5.equals("")&& !textvedNote4.equals("")) {
                Log.e(TAG,"textvedNote5: Inside "+textvedNote4.length());
                SavePreferences("NOTE5", note.getText().toString());
                LoadPreferences();
                note.setText("");
            } else if (textvedNote6.equals("")&& !textvedNote5.equals("")) {
                SavePreferences("NOTE6", note.getText().toString());
                Log.e(TAG,"textvedNote6: Inside "+textvedNote5.length());
                LoadPreferences();
                note.setText("");
            }
        } else if (view == clearText) {
            ClearTextViews();
        }else if (view == ClearAll){
            ClearTextViews();
            textSavedNote1.setText("");
            textSavedNote2.setText("");
            textSavedNote3.setText("");         
            textSavedNote4.setText("");         
            textSavedNote5.setText("");
            textSavedNote6.setText("");
        }

    }
    private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
    private void LoadPreferences(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String strSavedNote1 = sharedPreferences.getString("NOTE1", "");
        String strSavedNote2 = sharedPreferences.getString("NOTE2", "");
        String strSavedNote3 = sharedPreferences.getString("NOTE3", "");
        String strSavedNote4 = sharedPreferences.getString("NOTE4", "");
        String strSavedNote5 = sharedPreferences.getString("NOTE5", "");
        String strSavedNote6 = sharedPreferences.getString("NOTE6", "");
        if (!strSavedNote1.equals("")) {
            Log.e(TAG,"LoadPreferences1: "+strSavedNote1);
            textSavedNote1.setText(strSavedNote1);
        } else if (!strSavedNote2.equals("")) {
            Log.e(TAG,"LoadPreferences2:  "+strSavedNote2);
            textSavedNote2.setText(strSavedNote2);
        } else if (!strSavedNote3.equals("")) {
            Log.e(TAG,"LoadPreferences3:  "+strSavedNote3);
            textSavedNote3.setText(strSavedNote3);
        } else if (!strSavedNote4.equals("")) {
            textSavedNote4.setText(strSavedNote4);
        } else if (!strSavedNote5.equals("")) {
            textSavedNote5.setText(strSavedNote5);
        } else if (!strSavedNote6.equals("")) {
            textSavedNote6.setText(strSavedNote6);
        }
    }
    private void ClearTextViews(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM