gpt4 book ai didi

java - 将 JSON 保存到 Sqlite 并显示

转载 作者:行者123 更新时间:2023-12-01 14:27:55 30 4
gpt4 key购买 nike

我已经使用 JSONObject 成功访问了外部数据库中的数据,但我的问题是我要将其保存到 sqlite 数据库中。当我检索数据时,我只从 sqlite 中获取了最后一行。主要问题是从 sqlite 获取数据而不是 JSON,也许我在分配或检索时出错了.. 这里是菜鸟状态.. 谢谢

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

最佳答案

就是因为这个

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 键在 map 中插入所有记录。因此,每次将新条目添加到 map 时,每条记录都会被覆盖。因此,您只能看到 map 中的最后一条记录。

相反,将您的PID作为键,将NAME作为值(假设ID是唯一的),如果不是,您可以简单地使用对象的ArrayList ,而不是将其转换为 map 。

您需要这样做:-

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

关于java - 将 JSON 保存到 Sqlite 并显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17039370/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com