Saving ArrayList in SQLite database in Android

后端 未结 7 1662
臣服心动
臣服心动 2020-12-04 18:11

I\'ve been working with SQLite on android and I would like to add an arraylist to a column in a table, and then fetch the data back as an arraylist. The arraylist is a list

相关标签:
7条回答
  • 2020-12-04 19:08

    To Insert :

    ArrayList<String> inputArray=new ArrayList<String>();
    

    //....Add Values to inputArray

    Gson gson = new Gson();
    
    String inputString= gson.toJson(inputArray);
    
    System.out.println("inputString= " + inputString);
    

    use "inputString" to save the value of ArrayList in SQLite Database

    To retreive:

    Get the String from the SQLiteDatabse what you saved and changed into ArrayList type like below: outputarray is a String which is get from SQLiteDatabase for this example.

    Type type = new TypeToken<ArrayList<String>>() {}.getType();
    
    ArrayList<String> finalOutputString = gson.fromJson(outputarray, type);
    
    • In SQLite use text as format to store the string Value.....
    0 讨论(0)
提交回复
热议问题