Android: how to store an array of strings in SharedPreferences for Android

I am creating an application that searches the web using a search engine. I have one edittext in my application from which the user will search the web. I want to keep search keywords the same as browser history. I can save and display it with the last keyword, but I cannot increase the number of search results. I want to display the last 5 searches. Here is my code:

public class MainActivity extends Activity implements OnClickListener { Button insert; EditText edt; TextView txt1, txt2, txt3, txt4, txt5; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt = (EditText) findViewById(R.id.search_word); txt1 = (TextView) findViewById(R.id.txt1); txt1.setOnClickListener(this); insert = (Button) findViewById(R.id.insert); insert.setOnClickListener(new OnClickListener() { public void onClick(View v) { if ((edt.getText().toString().equals(""))) { Toast.makeText( getBaseContext(), "Whoa! You haven't entered anything in the search box.", Toast.LENGTH_SHORT).show(); } else { SharedPreferences app_preferences = PreferenceManager .getDefaultSharedPreferences(MainActivity.this); SharedPreferences.Editor editor = app_preferences.edit(); String text = edt.getText().toString(); editor.putString("key", text); editor.commit(); Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG) .show(); } } }); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); SharedPreferences app_preferences = PreferenceManager .getDefaultSharedPreferences(this); String text = app_preferences.getString("key", "null"); txt1.setText(text); } public void onClick(View v) { String text = txt1.getText().toString(); Toast.makeText(getBaseContext(), text, Toast.LENGTH_SHORT).show(); } 

}

Please help me in overcoming this problem.

+4
source share
2 answers

SAVE ARRAY

 public boolean saveArray(String[] array, String arrayName, Context mContext) { SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0); SharedPreferences.Editor editor = prefs.edit(); editor.putInt(arrayName +"_size", array.length); for(int i=0;i<array.length;i++) editor.putString(arrayName + "_" + i, array[i]); return editor.commit(); } 

OUTDOOR MASS

 public String[] loadArray(String arrayName, Context mContext) { SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0); int size = prefs.getInt(arrayName + "_size", 0); String array[] = new String[size]; for(int i=0;i<size;i++) array[i] = prefs.getString(arrayName + "_" + i, null); return array; } 
+31
source

Convert your array or object to Json with the Gson library and save the data as String in json format.

Save

 SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); Editor editor = sharedPrefs.edit(); Gson gson = new Gson(); String json = gson.toJson(arrayList); editor.putString(TAG, json); editor.commit(); 

Reading;

 SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); Gson gson = new Gson(); String json = sharedPrefs.getString(TAG, null); Type type = new TypeToken<ArrayList<ArrayObject>>() {}.getType(); ArrayList<ArrayObject> arrayList = gson.fromJson(json, type); 

Original answer: Saving an array object in SharedPreferences

0
source

Source: https://habr.com/ru/post/1433344/


All Articles