0

How can I set id.setAdapter(adapter); from SharedPreferences? I would like that when app close all id.getText().toString() should be present

ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
private SharedPreferences atPrefs;
atPrefs = PreferenceManager.getDefaultSharedPreferences(Login.this);
id = (AutoCompleteTextView) findViewById(R.id.id);

adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_expandable_list_item_1, list);
id.setAdapter(adapter);
adapter.setNotifyOnChange(true);
id.setThreshold(1);

submit.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View arg0) {
        if (id.getText().toString().length() >= 3) {
            
            atPrefs.edit().putString(pos, id.getText().toString()).commit();
            adapter.add(atPrefs.getString(pos, ""));
            
            Intent nextscreen = new Intent(Login.this, Otp.class);
            startActivity(nextscreen);
            // finish();
        }
    }
});

Solved

solved with following code

for (i = 0; i < k; i++) {

    list.add(0, (String) prfs.getString(String.valueOf(i), "null"));
    adapter.notifyDataSetChanged();
}

public void onClick(View arg0) {
    if (id.getText().toString().length() >= 3) {
    
        list.add(0, (String) id.getText().toString());
        adapter.notifyDataSetChanged();
        
        edit.putString(String.valueOf(i), id.getText().toString());
        edit.putInt("counter", i + 1);
        edit.apply();
        
        id.setText("");
        
        i = i + 1;
    }
Community
  • 1
  • 1
user3244282
  • 91
  • 2
  • 10

1 Answers1

0

You should check out this answer, keeping values in SharedPreferences is not such a good idea.

Also you are not using shared preferences correctly, you only save the value in and never get it back using _context.getString.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putStringArray("myarray", list);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  list = savedInstanceState.getStringArray("myarry");

}
Community
  • 1
  • 1
Dan Cuc
  • 76
  • 4
  • Essentially it would be the same argument as using global variables, it should be avoided. The data you are storing is Activity specific, not application specific so every time you get a reference to Shared Preferences you load in memory all the values saved in it. When you will have more activities, you will end up loading unnecessary values of other activities that are in the background, not to mention that everyone other activity is allowed to make changes to the values you saved. – Dan Cuc Mar 04 '14 at 12:18
  • den i used putStringArrayList but still i am not getting data when i close app and open it again – user3244282 Mar 04 '14 at 12:31
  • I disagree - SP are ideal for this - and keeping the keys private should help you minimize the activity interaction – Mr_and_Mrs_D Mar 04 '14 at 15:25