SQLite只获取最后一条记录而不是所有记录

时间:2016-08-09 13:31:06

标签: android sqlite arraylist cursor android-sqlite

我在SQlite数据库中有20条记录 当我调用GetOfflineRecords()方法时,它只获得20次最后一次记录 我希望得到所有记录。但它的最后记录是20次。

This link is use to get last row. I wish to get All rows

public void GetOfflineRecords() {

        Log.d("Offline :", "Offline Data Retrive");

        String selectQueryFinal = "SELECT * FROM " + AppDatabase.TABLE_MARKER_LIST;

        Log.d("SelectQuerry", selectQueryFinal);
        final Cursor cursorFinal = db.rawQuery(selectQueryFinal, null);
        Log.d("CurCount-Offline", cursorFinal.getCount() + " -crCountOffline");

        if (cursorFinal != null && cursorFinal.getCount() != 0) {
            if (cursorFinal.moveToFirst()) {
                do {
                    markerListResult = new Bean_Marker();
                    markerListResult.setId(cursorFinal.getString(cursorFinal.getColumnIndex("id")));
                    markerListResult.setPlace_name(cursorFinal.getString(cursorFinal.getColumnIndex("place_name")));
                    markerListResult.setLatitude(cursorFinal.getString(cursorFinal.getColumnIndex("latitude")));
                    markerListResult.setLongitude(cursorFinal.getString(cursorFinal.getColumnIndex("longitude")));
                    markerListResult.setShort_description(cursorFinal.getString(cursorFinal.getColumnIndex("short_desctiption")));

                    Log.e("ID : ", cursorFinal.getString(cursorFinal.getColumnIndex("id")));
                    Log.e("Place Name : ", cursorFinal.getString(cursorFinal.getColumnIndex("place_name")));
                    markerList.add(markerListResult);
                } while (cursorFinal.moveToNext());

            }

        } else { // No Records are there
            Log.e("Cursor", "Cursor is empty");
            //dialogMessage.SingleButtonDialog(OfflineMap.this, "No Data Alert", "You have no offline data found");
        }

        //listSurvey.setAdapter(new AdapterSurveyList(SurveyList.this, survey_listList, callClickListener, cameraClickListener));
    }

1 个答案:

答案 0 :(得分:0)

在实现一次检查光标长度之前,如果确定那么调试光标循环并检查每次(每个索引)你从查询中获得正确的记录。

如果一切正常,请使用下面的代码段。

因为问题可能是从光标获取记录。

使用以下代码检查:

public void GetOfflineRecords() {

        String selectQueryFinal = "SELECT * FROM " + AppDatabase.TABLE_MARKER_LIST;

        Log.d("SelectQuerry", selectQueryFinal);
        final Cursor cursorFinal = db.rawQuery(selectQueryFinal, null);
        Log.d("CurCount-Offline", cursorFinal.getCount() + " -crCountOffline");//Check length over here

        ArrayList<Bean_Marker> markerList = new ArrayList<>();
        if (cursorFinal != null && cursorFinal.getCount() != 0) {
            if (cursorFinal.moveToFirst()) {
                do {
                    markerListResult = new Bean_Marker();
                    markerListResult.setId(cursorFinal.getString(cursorFinal.getColumnIndex("id")));
                    markerListResult.setPlace_name(cursorFinal.getString(cursorFinal.getColumnIndex("place_name")));
                    markerListResult.setLatitude(cursorFinal.getString(cursorFinal.getColumnIndex("latitude")));
                    markerListResult.setLongitude(cursorFinal.getString(cursorFinal.getColumnIndex("longitude")));
                    markerListResult.setShort_description(cursorFinal.getString(cursorFinal.getColumnIndex("short_desctiption")));

                    Log.e("ID : ", cursorFinal.getString(cursorFinal.getColumnIndex("id")));
                    Log.e("Place Name : ", cursorFinal.getString(cursorFinal.getColumnIndex("place_name")));
                    markerList.add(markerListResult);
                } while (cursorFinal.moveToNext());

            }

        } else { // No Records are there
            Log.e("Cursor", "Cursor is empty");

        }
        Log.e("Cursor", "markerList length"+markerList.size());
////Check length over here

    }

如果仍然无效,则直接使用列索引

  markerListResult.setId(cursorFinal.getString(0));