Save HashMap data to SQLite

I am trying to save data from Json

to SQLite

. While I am saving data from Json

to HashMap

.

I'm already looking at it, and they say to use it ContentValues

. But I still don't understand how to use it.

I am trying to address this issue of persisting data to SQLite from json object using Hashmap in Android , but it doesn't help much.

Is there any parameter I can use to save data from HashMap

to SQLite

?

Here's my code.

MainHellobali.java

// Hashmap for ListView
ArrayList<HashMap<String, String>> all_itemList; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main_helloballi);

    all_itemList = new ArrayList<HashMap<String, String>>();
    // Calling async task to get json
    new getAllItem().execute();
}


private class getAllItem extends AsyncTask<Void, Void, Void> {


    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {

                all_item = new JSONArray(jsonStr);


                // looping through All Contacts
                for (int i = 0; i < all_item.length(); i++) {
                    JSONObject c = all_item.getJSONObject(i);

                        String item_id = c.getString(TAG_ITEM_ID);
                        String category_name = c.getString(TAG_CATEGORY_NAME);
                        String item_name = c.getString(TAG_ITEM_NAME);

                        // tmp hashmap for single contact
                        HashMap<String, String> allItem = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        allItem.put(TAG_ITEM_ID, item_id);
                        allItem.put(TAG_CATEGORY_NAME, category_name);
                        allItem.put(TAG_ITEM_NAME, item_name);  

                        // adding contact to contact list
                        all_itemList.add(allItem);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }
}

      

I have DatabasehHandler.java

and AllItem.java

. I can put it here if needed.

Thanks before

** Add modified code **

 // looping through All Contacts
                for (int i = 0; i < all_item.length(); i++) {
                    JSONObject c = all_item.getJSONObject(i);

                        String item_id = c.getString(TAG_ITEM_ID);
                        String category_name = c.getString(TAG_CATEGORY_NAME);
                        String item_name = c.getString(TAG_ITEM_NAME);

                        DatabaseHandler databaseHandler = new DatabaseHandler(this); //error here "The Constructor DatabaseHandler(MainHellobali.getAllItem) is undefined

}

      

+3


source to share


2 answers


As @birdy mentioned, you can just store the JSON data as String inside your database. In my case, I have already done the exact same thing that you are trying to achieve, in my case I just created an abstract datasource that will extend for any JSON object that will be set in my database. But basically you only need a way to convert the JSONObject to ContentValues.

public ContentValues jsonToContentValues(JSONObject json) {
        ContentValues values = new ContentValues();
        values.put("MY_COLUMN", json.optString("MY_JSON_VALUE"));
        return values;
}

      



Once you have set your content value object, you just need to insert the values ​​into your database.

return database.insert("MY_TABLE_NAME", null, contentValues);

      

+2


source


If you need to store data JSON

, just save it as text. Then, after returning from the database, you can analyze it again on the map.



+1


source







All Articles