Search code examples
androidjsondatabasesqliteretrofit

How to save JSON list to SQLite and display to listview


I have already succeeded in parsing JSON, now I want to save the result of JSON to SQLite. My problem is I don't know how to save JSON list to SQLite. Please help me if you know how to save JSON to SQLite. Thanks in advance..

Btw, this is the json

[
  {
    "UserId": "3c652fc4-5dc4-4fa5-8c07-d27b0b657823",
    "Nationality": "ID",
    "Image": null,
    "IsAlreadyOnTextoaser": true,
    "IsAlreadyOnRequest": false,
    "LastActive": null,
    "LastActiveResume": "0 Days Ago",
    "FirstName": "dwi",
    "LastName": "wahyudi"
  },
  {
    "UserId": "cbb1f8bd-8016-4bc6-abd1-701fb353293d",
    "Nationality": "ID",
    "Image": null,
    "IsAlreadyOnTextoaser": true,
    "IsAlreadyOnRequest": false,
    "LastActive": null,
    "LastActiveResume": "0 Days Ago",
    "FirstName": "Wildan",
    "LastName": "Rachman"
  }
]

Solution

  • Let me guide you step by step!

    you get a JsonArray as a response and you want to store in SQLite locally. This Link has answers all the code related stuff I will only walk you through the process.

    You need to create database and table with appropriate number of columns and data type.

    In SQLliteHelper create two public method one to add data and second to retrieve to display in the list.

    Create instance of SQLliteHelper where you have access to the list and call method to add to the database and in your view where you want to display it call second method to retrieve the data from sqlite.

    if you do not know how to use either listview or recyclerview please comment