If data is static save it in Strings.xml, else you can save it in DB and access it. Here is the link that will help you http://www.giantflyingsaucer.com/blog/?p=1342
AutoCompleteTextView in android with shared preferences
-
10-06-2023 - |
Question
In my application I need to show list of user who are logging in. In my username text field. I tried to store in sharedpreferences and retrieve but I couldn't. How can I get it done?
I parsed the array to json and stored it in an array then used in a arrayadapter.
Here is my code:
public class MainActivity extends Activity {
SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
public static String user_id;
EditText etxt_user;
String PLAYLISTS;
String[] listt;
ArrayList<String> array;
JSONArray jArray;
@SuppressLint("CutPasteId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etxt_user = (EditText) findViewById(R.id.textView1);
user_id = etxt_user.getText().toString();
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
final Editor editor = sharedpreferences.edit();
array = new ArrayList<String>();
jArray = new JSONArray(array);
// final JSONArray jArray = new JSONArray(array);
// editor.putString("buttonArray", jArray.toString());
// editor.commit();
final ArrayList<String> array1 = new ArrayList<String>();
// final String name = sharedpreferences.getString("buttonArray", null);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, array);
final AutoCompleteTextView textView = (AutoCompleteTextView)findViewById(R.id.textView1);
textView.setAdapter(adapter);
Button b1= (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
array.add(etxt_user.getText().toString());
editor.putString("buttonArray", jArray.toString());
editor.commit();
String name = sharedpreferences.getString("buttonArray", null);
System.out.println("222222222222"+jArray.toString());
System.out.println("333333333333"+name);
System.out.println("444444444444"+array);
editor.putString(PLAYLISTS,etxt_user.getText().toString());
}
});
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow