将json数据存储到sqlite并检索到recycleler视图

时间:2018-02-22 05:02:34

标签: android json sqlite android-recyclerview

我是Android的新手,我正在尝试将json数据添加到sqlite数据库并检索到recyclerview。但我只在recyclerview获取第一个数据(但json数组中有7个数据)。请任何人帮我这样做。我的代码发布在下面。

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

        } 

2 个答案:

答案 0 :(得分:1)

cursor.moveToFirst();

而不是这个用途

if (cursor.moveToFirst()) {
   do {

    // your data code

} while (cursor.moveToNext());

答案 1 :(得分:0)

试试这个

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