Save HashMap data into SQLite

Posted by Matthew on Stack Overflow See other posts from Stack Overflow or by Matthew
Published on 2014-08-21T16:01:06Z Indexed on 2014/08/21 16:20 UTC
Read the original article Hit count: 286

Filed under:
|
|
|
|

I'm Trying to save data from Json into SQLite. For now I keep the data from Json into HashMap.

I already search it, and there's said use the ContentValues. But I still don't get it how to use it.

I try looking at this question save data to SQLite from json object using Hashmap in Android, but it doesn't help a lot.

Is there any option that I can use to save the data from HashMap into 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 too. I can put it in here if its necessary.

Thanks before

** Add Edited 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

}

© Stack Overflow or respective owner

Related posts about java

Related posts about android