Android: Cómo almacenar una matriz de cadenas en SharedPreferences para android

Estoy construyendo una aplicación que busca en la web utilizando el motor de búsqueda. Tengo un edittext en mi aplicación de la que el usuario buscará en la web. Quiero guardar las palabras clave de búsqueda como lo hace el historial del navegador. Puedo guardarlo y mostrarlo con la última palabra clave, pero no puedo aumentar el número de resultados de búsqueda. Quiero mostrar los últimos 5 searhes. Aquí está mi código:

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

}

Por favor, ayúdame a superar este problema.

GUARDAR 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(); } 

CARGA DE LA CARGA

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

Convierta su matriz o objeto a Json con la biblioteca Gson y almacene sus datos como String en formato json.

Salvar;

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

Leer;

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

Respuesta original: Almacenar objeto de lista de matrices en SharedPreferences

  • Configuración de eclipse para quejarse de cadenas codificadas duras para las preferencias de Android xml
  • ¿Cómo se declara el tipo de preferencia de Android?
  • Plugin ADT para Android no aparece en Eclipse
  • Usar TimePicker o DatePicker en la pantalla Preferencias en Android
  • Android: ¿Cómo ver el archivo SharedPreferences?
  • SharedPreferences Editor commit lleva mucho tiempo
  • Las preferencias compartidas sólo se guardan por primera vez
  • Preferencias de Android: ¿Cómo cargar los valores predeterminados cuando el usuario no ha utilizado la pantalla de preferencias?
  • Preferencias anidadas.xml
  • ¿Por qué Custom DialogPreference no se activa en onSharedPreferenceChanged?
  • Android: ¿Cómo restablecer / eliminar las preferencias de la aplicación durante las pruebas de unidad?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.