Saving object list to Android

1

I want to save this list (List) after exiting the application, and when I run the application again I want to retrieve the instance from the list that I had previously saved.

private List<BluetoothDevice> listaDispositivoFavorito = new ArrayList<BluetoothDevice>();
    
asked by anonymous 24.09.2014 / 05:08

1 answer

2

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

The most commonly used are:

SharedPreferences

Which is a place where you can save values     primitives, in the key-value pairs format.

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

You can use the gson library 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

What is a SQL database for Android. How to use here .

References:

  • link
  • link
  • link
  • link
  • link
  • 24.09.2014 / 13:41
    ___ ___ erkimt Copy Project in NodeJS to another machine ______ qstntxt ___

    I have NodeJS installed on two machines.

    I need to migrate the project from one machine to another.

    Just copy the project folder or on the target machine will I have to do something?

    I installed several Express 4 modules, do I have to reinstall on the new machine or copy the folder just enough?

    My concern is that further bugs will emerge due to poor migration.

        
    ______ ___ azszpr33504

    Just copy only the project folder or in your destination will have to do something?

    If your project uses %code% by copying the entire source project and run a %code% that everything is ready .

        
    ___ ___ ___ erkimt Error adding HttpPost the controller ______ qstntxt ___

    I have a problem in time to add the attribute %code% on my controller. I mean, I can add the attribute without problems, but when I compile and go to the form registration I just can not access.

    I get the following error:

      

    Server Error in Application '/'.

         

    Can not find the resource.

         

    Description: HTTP 404. The resource you are looking for (or one of its dependencies) could not be removed, had its name changed, or is temporarily unavailable. Examine the URL and make sure that it is spelled correctly.

         

    requested URL: / User / add

    Version Information: Microsoft .NET Framework Version: 4.0.30319; ASP.NET:4.0.30319.33440

    Also, when I take %code% % attribute, I can not see the registration form, but I can not do the insert in the bank.

        
    ______ ___ azszpr31034

    When you add the HttpPost attribute in an action. It can only be accessed via the HTTP POST method.

    If your intention is to only display the form. Do not decorate your method with no attributes, the default is GET.

    What you can do is set an Add method with no attribute. This method will be accessed via GET and display the form.

    Then you create a second method, for example AdicionarConfirmado, decorated with the attributes and HttpPost ActionName="Add"

    The methods can no defined as follows

    %pre%

    Since your form that will save the record would look like this:

    %pre%     
    ___