How to save objects list in Android

Asked

Viewed 1,351 times

1

I want to save this list (List) after exiting the application, and when running again I want to recover the instance from the list that was previously saved.

private List<BluetoothDevice> listaDispositivoFavorito = new ArrayList<BluetoothDevice>();
  • Ta is what Bluetooth has to do with history? I didn’t understand anything.

  • To make an application q need to save the list List<Bluetoothdevice> listFispositiveFavorite = new Arraylist<Bluetoothdevice>(); and then recover when the application runs again

  • I get it, there are some ways to record data on Android such as those.

  • Welcome to Stackoverflow in Portuguese. I believe you should elaborate your question better, I recommend you read "How to ask a good question?" and edit your question to make it easier to understand what you want.

1 answer

2


There are some ways to record/persist data on Android to retrieve it later, as demonstrated here in the documentation:

The most commonly used are:

Sharedpreferences

Which is a place where you can store values primitive, key-value format (key-value pairs).

In your case you can serialize the object in json (in String), and retrieve and deserialize it later.

You can use the library gson to serialize and deserialize the object.

To save:

SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);
Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(listaDispositivoFavorito );
prefsEditor.putString("DISPOSITIVOS", json);
prefsEditor.commit();

To recover:

SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);
Gson gson = new Gson();
String json = mPrefs.getString("DISPOSITIVOS", "");
Type type = new TypeToken<List<BluetoothDevice>>(){}.getType();
List<BluetoothDevice> obj = gson.fromJson(json, type);

Sqlite Databases

Which is an SQL database for Android. How to use here.

References:

  1. http://developer.android.com/guide/topics/data/data-storage.html#pref
  2. http://developer.android.com/guide/topics/data/data-storage.html#db
  3. https://stackoverflow.com/questions/7145606/how-android-sharedpreferences-save-store-object
  4. https://stackoverflow.com/questions/20819294/save-custom-object-array-to-shared-preferences
  5. https://stackoverflow.com/questions/14981233/android-arraylist-of-custom-objects-save-to-sharedpreferences-serializable
  • estou conseguindo salvar mas ao realizar o seguinte codigo para recuperar: SharedPreferences save = getSharedPreferences("save", MODE_PRIVATE); Gson gson = new Gson();&#xA;String json = save.getString("DISPOSITIVOS", "");&#xA;Type type = new TypeToken<List<BluetoothDevice>>(){}. getType(); List<Bluetoothdevice> obj = gson.fromJson(json, type); for (Bluetoothdevice device : obj) { Toast.makeText(this, device.getName(),Toast.LENGTH_SHORT). show(); }

  • i printei : Toast.makeText(this,json,Toast.LENGTH_SHORT). show(); and runs normally, i.e., there is the object but at the time of running Type type = new Typetoken<List<Bluetoothdevice>>(){}. getType(); List<Bluetoothdevice> obj = gson.fromJson(json, type); error

  • gave error : at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584) at dalvik.system.NativeStart.main(Native Method): Caused by: java.lang.NullPointerException at android.bluetooth.Bluetoothdevice.getName(Bluetoothdevice.java:579)

  • @Fernando I don’t think it can be done that way, the class BluetoothDevice is a Memento, he only possessed the attribute mAddress and its constructor is package. You would have to use a Parcelable to save him.

  • @Wakim strange and that the execute Toast.makeText(this, device.getAdress(), Toast.LENGTH_SHORT). show(); I just couldn’t get the device.getName() method... but the object is still "correctly deserialized"?

  • @Pedrorangel, the getName wrong because he uses the BluetoothService, which is only initialized in the constructor, calling the method getService. Have a look at the source code: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/bluetooth/BluetoothDevice.java#Bluetoothdevice.%3Cinit%3E%28java.lang.String%29.

  • @True Wakim... thank you, but if I try to use this object that has been deserialized to communicate with other devices may occur problems per said account?

  • @Pedrorangel, revolved? Only mark as answer when really solved.

  • @Pedrorangel, you can serialize the addresses (getAddress()) and call the BluetoothAdapter.getRemoteDevice(java.lang.String) passing the serialized address. And to recover the BluetoothAdapter use BluetoothAdapter.getDefaultAdapter(). More details on: http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice(java.lang.String)

  • @Wakim, in case serialize a List<String>, and then redo the Bluetooth object, right?

  • That’s right @Fernando. If you could include that in your reply it would be nice

  • @Fernando did. I don’t think I need to serialize... I’ve been getting so far what I wanted... my doubt was only on the question of getName() which is not serialized by using the Bluetoothservice as Wakim had previously said, so only "part" of the Bluetoothdevice object is serialized

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.