0
class A{
String str1="";
String str2="";
ArrayList<B> barr=new ArrayList<B>();

}


class B{
int a;
double d;
}


**ArrayList<A> aArr=new ArrayList<A>();**

I want to store the aArr list in sqlite db in a column and retrieve it.

I think I can convert it to JSON String. How to do it?

Any one help me out .

VaaS
  • 637
  • 2
  • 12
  • 18

3 Answers3

0

If you are already using Gson, or planning to use it, then I would recommend you to convert your list object into Json string and then store it in a Text type column in SQLite. Its also pretty much easy to reconstruct your object from json string using the same library. Thats just a suggestion from me as I personally like to handle strings rather than playing with bytes.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • Read the guide, which contains all the tutorials https://sites.google.com/site/gson/gson-user-guide – waqaslam Jun 13 '12 at 07:52
0

There is no build in function to to that, especially when storing the arraylist in just 1 column. You could convert your class A to a string, and separate the data by specific characters, how ever that is not the right way to go.

You need a new database system, something like this:

Your table:

Your Column | Some other Column | and another Column | ArraylistA_id

ArraylistA table:

ArraylistA_id | String1 | String2 | ArraylistB_id

ArraylistB table:

ArraylistB_id | int | double

In this database system the columns "ArraylistA id" and "ArraylistB id", are pointers to other rows in another table. So for example when your arraylistA exists out of the following:

Arraylist A{

{"TEST", "TEST", ArrayListB{{1,1.0}, {2, 2.0}, {3, 3.0}}}

{"HELLO", "HELLO", ArrayListB{{9,9.0}, {8, 8.0}, {7, 7.0}}}

};

Than the records in the sql database would be the following:

Your table:

Your Column | Some other Column | and another Column | ArraylistA_id

                                                           1

ArraylistA table:

ArraylistA_id | String1 | String2 | ArraylistB_id

   1           TEST      TEST          1

   2           HELLO     HELLO         2

ArraylistB table:

ArraylistB_id | int | double

  1            1      1.0

  1            2      2.0

  1            3      3.0

  2            9      9.0

  2            8      8.0

  2            7      7.0

The point of the complete story is that you never should store an arraylist like this in just one column, you need a proper database system before doing something like this. Made this on my tablet in the train, when i arrive at school i will clean it up a bit.

Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
0

You can try this method.

public long insertString(String newString) 
  {
    ContentValues newStringValue = new ContentValues();

    newStringValue.put(KEY_STRING, newString);

    return db.insert(DATABASE_TABLE, null, newStringValue);
  }
Ankur
  • 5,086
  • 19
  • 37
  • 62
Ravi
  • 31
  • 3