将 json 数据存储到 sqlite 并检索到回收站视图
Posted
技术标签:
【中文标题】将 json 数据存储到 sqlite 并检索到回收站视图【英文标题】:storing json data to sqlite and retrieving to recycler view 【发布时间】:2018-08-01 20:44:35 【问题描述】:我是android新手,我正在尝试将json
数据添加到sqlite
数据库并检索到recyclerview
。但我只在recyclerview
中获得第一个数据。(但有7个数据数据在json
数组中)。请任何人帮助我做到这一点。我的代码发布在下面。
try
JSONObject json_data = new JSONObject(result);
JSONArray contacts = json_data.getJSONArray("contacts");
for (int i = 0; i < contacts.length(); i++)
JSONObject c = contacts.getJSONObject(i);
Fullname = c.getString("name");
Email = c.getString("email");
Address = c.getString("address");
Gender = c.getString("gender");
/* InsertData(c.getString("name"), c.getString("email"), c.getString("address"),
c.getString("gender"));*/
InsertData(Fullname, Email, Address, Gender);
Log.d("11111111", Fullname);
openHelper = new SQLiteDBHelper(MainActivity.this);
db = openHelper.getReadableDatabase();
cursor = db.rawQuery("SELECT *from " + SQLiteDBHelper.TABLE_NAME, null);
if (cursor != null)
if (cursor.getCount() > 0)
cursor.moveToFirst();
DataFish fishData = new DataFish();
//Retrieving User FullName and Email after successfull login and passing to LoginSucessActivity
fishData.fishName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME));
fishData.catName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_EMAIL));
fishData.sizeName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_ADDRESS));
fishData.price = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_GEDER));
// Log.d("NAME", cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME)));
f=cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME));
Log.d("NAMEE",f);
data.add(fishData);
// Setup and Handover data to recyclerview
mRVFishPrice = (RecyclerView) findViewById(R.id.fishPriceList);
mAdapter = new AdapterFish(MainActivity.this, data);
mRVFishPrice.setAdapter(mAdapter);
mRVFishPrice.setLayoutManager(new LinearLayoutManager(MainActivity.this));
catch (JSONException e)
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
public void InsertData(String fullName, String email, String adress, String gender )
db = openHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(SQLiteDBHelper.COLUMN_FULLNAME,fullName);
values.put(SQLiteDBHelper.COLUMN_EMAIL,email);
values.put(SQLiteDBHelper.COLUMN_ADDRESS,adress);
values.put(SQLiteDBHelper.COLUMN_GEDER,gender);
long id = db.insert(SQLiteDBHelper.TABLE_NAME,null,values);
【问题讨论】:
【参考方案1】:cursor.moveToFirst();
而不是这个用途
if (cursor.moveToFirst())
do
// your data code
while (cursor.moveToNext());
【讨论】:
另外请检查 openHelper 是否在 InsertData 方法之前初始化。 非常感谢 mahn...您节省了我的时间【参考方案2】:试试这个
if (cursor.moveToFirst())
while (!cursor.isAfterLast())
DataFish fishData = new DataFish();
//Retrieving User FullName and Email after successfull login and passing to LoginSucessActivity
fishData.fishName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME));
fishData.catName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_EMAIL));
fishData.sizeName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_ADDRESS));
fishData.price = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_GEDER));
// Log.d("NAME", cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME)));
f = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME));
Log.d("NAMEE", f);
data.add(fishData);
cursor.moveToNext();
或者这个
if (cursor.moveToFirst())
do
DataFish fishData = new DataFish();
//Retrieving User FullName and Email after successfull login and passing to LoginSucessActivity
fishData.fishName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME));
fishData.catName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_EMAIL));
fishData.sizeName = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_ADDRESS));
fishData.price = cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_GEDER));
// Log.d("NAME", cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME)));
f=cursor.getString(cursor.getColumnIndex(SQLiteDBHelper.COLUMN_FULLNAME));
Log.d("NAMEE",f);
data.add(fishData);
while (cursor.moveToNext());
【讨论】:
以上是关于将 json 数据存储到 sqlite 并检索到回收站视图的主要内容,如果未能解决你的问题,请参考以下文章
从 SQLite 中检索 JSON 数据时,反斜杠显示在数组中
使用 swiftyJSON 和 sqlite.swift 将基于 json 的数据添加到 sqlite 数据库中
如何将图像存储为 Sqlite 中的 blob 以及如何检索它?