rahstame
rahstame

Reputation: 2178

Saving JSON to Sqlite and displaying it

I have successfully accessed the data from an external datbase using JSONObject, but my problem is that I am going to save it to the sqlite database. By the time that I retrieve the data I only got the last row from sqlite. The main issue is getting the data from sqlite not JSON, maybe I got wrong in assigning or retrieving.. noobness state here..thanks

CODE BEFORE ADDING THE SQLITE CODE:

                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        String name = c.getString(TAG_NAME);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_NAME, name);

                        // adding HashList to ArrayList
                        productsList.add(map);

Adding to SQlite:

           { // products found
            // Getting Array of Products
        products = json.getJSONArray(TAG_PRODUCTS);

           // looping through All Products
            for (int i = 0; i < products.length(); i++) {

            //individually get each arrays
            JSONObject c = products.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString(TAG_PID);
            String name = c.getString(TAG_NAME);

             //code to add each retrieved data from JSONArray to Sqlite
             db.addContact(new Menu(id.toString(), name.toString()));

        }
         // Reading all contacts
        Log.d("Reading: ", "Reading all contacts..");
        List<Menu> contacts = db.getAllContacts(); 

        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();

        for (Menu cn : contacts) {

                // Writing Contacts to log

        // adding each child node to HashMap key => value
        map.put(TAG_PID, cn.getID().toString());
        map.put(TAG_NAME,cn.getName().toString());

        // adding HashList to ArrayList
        productsList.add(map);
        }

Another Part of the Program:

ListAdapter adapter = new SimpleAdapter(
                                Main_Activity.this, productsList,
                                R.layout.activity_view_products, new String[] { TAG_PID,
                                        TAG_NAME},
                                new int[] { R.id.pid, R.id.name });
                        // updating listview
                        setListAdapter(adapter);

Another Code:

// Adding new contact
void addContact(Menu contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ID, contact.getID()); // Contact Name
    values.put(KEY_NAME, contact.getName()); // Contact Phone

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}
  // Getting All Contacts
    public List<Menu> getAllContacts() {
    List<Menu> contactList = new ArrayList<Menu>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Menu contact = new Menu();
            contact.setID((cursor.getString(0).toString()));
            contact.setName(cursor.getString(1).toString());

            Log.d("Menu: ", contact.getID().toString());

            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

Upvotes: 0

Views: 1299

Answers (1)

Rahul
Rahul

Reputation: 45060

Its because of this

map.put(TAG_PID, cn.getID().toString()); // TAG_PID is the same for all entry id, hence, overrideen
map.put(TAG_NAME,cn.getName().toString()); // TAG_NAME is the same for all entry name, hence, overrideen    
// adding HashList to ArrayList
productsList.add(map);

You are inserting all the records in the map with the same PID and NAME keys. Hence, each record keeps getting overridden every time a new entry is added to the map. Hence, you can see only the last record in the map.

Instead, keep your PID as the key and NAME as the value(assuming ID is unique), if not you can simply use the ArrayList of your objects as such, instead of converting it to a Map.

Instead of this you need to do this:-

map.put(cn.getID().toString(), cn.getName().toString());

// adding HashList to ArrayList
productsList.add(map);

Update:- After seeing your requirement, I think this is what you need to do.

for (Menu cn : contacts) {

    // creating new HashMap - for every Menu
    HashMap<String, String> map = new HashMap<String, String>(); // this should be within the for loop.

    // Writing Contacts to log
    // adding each child node to HashMap key => value
    map.put(TAG_PID, cn.getID().toString());
    map.put(TAG_NAME, cn.getName().toString());

    // adding HashList to ArrayList
    productsList.add(map);
}

Upvotes: 1

Related Questions