将JSON保存到Sqlite并显示它

时间:2013-06-11 08:14:25

标签: java android json sqlite

我已经使用JSONObject成功访问了外部数据库中的数据,但我的问题是我要将它保存到sqlite数据库。当我检索数据时,我只从sqlite获得最后一行。主要问题是从sqlite获取数据而不是JSON,也许我在分配或检索时出错了.. noobness state here..thanks

添加SQLITE代码之前的代码:

                        // 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);

添加到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);
        }

该计划的另一部分:

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);

另一个代码:

// 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;
}

1 个答案:

答案 0 :(得分:1)

因为这个

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);

您正在使用相同的PIDNAME键在地图中插入所有记录。因此,每次将新条目添加到地图时,每个记录都会被覆盖。因此,您只能看到地图中的最后一条记录。

相反,请将PID作为键,NAME作为值(假设ID是唯一的),否则您只需使用对象的ArrayList,而不是将其转换为一张地图。

而不是这个你需要这样做: -

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

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

<强>更新: - 在看到您的要求后,我认为这就是您需要做的事情。

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);
}