android查询只返回最后一行

时间:2016-10-13 06:40:11

标签: android sqlite android-asynctask

我正在构建一个天气应用程序,它从openweathermap api获取数据并将它们插入到AsyncTask的doInBackgroudMethod中的数据库中。在onPostExecute中,我查询数据库表并将光标设置为SimpleCursorAdapter。问题在于:虽然bulkInsert显示已将14行插入到数据库中,但onPostExecute中的查询仅返回一行(如果我将使用从JSON返回的JSON值获得的值进行比较,则该行似乎是最后一行OWM服务器)。最后一行是我运行应用程序时在测试设备上显示的内容。这是我用来插入doInBackGround的代码。我应该补充一点,doInBackground获取数据,解析JSON并相应地格式化它。最后一步是我将14天的天气数据批量插入数据库,这似乎有效。由于insert()方法不返回-1或任何奇怪的错误。 query()不会返回它应该返回的内容。

   public int bulkInsert(Uri uri, ContentValues[] values) {

        final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        final int match = sUriMatcher.match(uri);
        switch (match) {
            case WEATHER:
                db.beginTransaction();
                int returnCount = 0;
                try {
                    for (ContentValues value : values) {
                        normalizeDate(value);
                        long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, value);

//this log increases with every insert. The logs are shown below
                        Log.d(MainActivity.TAG, " row id value? "+ _id);
                        if (_id != -1) {
                            returnCount++;
                        }
                    }
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
                }
                getContext().getContentResolver().notifyChange(uri, null);
                db.close();

                mOpenHelper.close();

                return returnCount;
            default:
                return super.bulkInsert(uri, values);
        }


}

我使用以下内容查询onPostExecute中的表

protected void onPostExecute(String[] strings) {
            super.onPostExecute(strings);
            mForecastAdapter.notifyDataSetChanged();
            Cursor countCursor = getContext().getContentResolver().query(WeatherEntry.CONTENT_URI, null, null, null, null);

            countCursor.moveToFirst();
            int count = countCursor.getCount();

            Log.d(MainActivity.TAG,"row count? "+count);

          if(countCursor.moveToFirst()){
              Log.d(MainActivity.TAG, "swapping cursor");

              mForecastAdapter.swapCursor(countCursor);
          }


            Log.d(MainActivity.TAG, "cursor?: "+countCursor.moveToFirst());

            String countQuery = "SELECT  * FROM " + WeatherEntry.TABLE_NAME;
            SQLiteDatabase db = new WeatherDbHelper(getContext()).getReadableDatabase();
            Cursor cursor = db.rawQuery(countQuery, null);
            int cnt = cursor.getCount();
            cursor.close();

            Log.d(MainActivity.TAG, " HOW MANY ROWS? "+cnt);

        }

这是我的内容提供商

中查询方法的实现
  @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
                        String sortOrder) {
        // Here's the switch statement that, given a URI, will determine what kind of request it is,
        // and query the database accordingly.
        Cursor retCursor;
        switch (sUriMatcher.match(uri)) {
            // "weather/*/*"
            case WEATHER_WITH_LOCATION_AND_DATE:
            {
                retCursor = getWeatherByLocationSettingAndDate(uri, projection, sortOrder);
                break;
            }
            // "weather/*"
            case WEATHER_WITH_LOCATION: {
                retCursor = getWeatherByLocationSetting(uri, projection, sortOrder);
                break;
            }
            // "weather"
            case WEATHER: {
                retCursor = mOpenHelper.getReadableDatabase().query(
                        WeatherContract.WeatherEntry.TABLE_NAME,
                        projection,
                        selection,
                        selectionArgs,
                        null,
                        null,
                        sortOrder
                );
                break;
            }
            // "location"
            case LOCATION: {
                retCursor = mOpenHelper.getReadableDatabase().query(
                        WeatherContract.LocationEntry.TABLE_NAME,
                        projection,
                        selection,
                        selectionArgs,
                        null,
                        null,
                        sortOrder
                );
                break;
            }

            default:
                throw new UnsupportedOperationException("Unknown uri: " + uri);
        }
        retCursor.setNotificationUri(getContext().getContentResolver(), uri);

        return retCursor;
    }

以下是日志:

10-13 09:13:08.416 27330-27330/com.example.android.mysunshine.app D/mainTag: onCreate called: 
10-13 09:13:08.424 27330-27330/com.example.android.mysunshine.app D/mainTag: ON START CALLED
10-13 09:13:08.426 27330-27330/com.example.android.mysunshine.app D/mainTag: 90210
10-13 09:13:10.604 27330-27355/com.example.android.mysunshine.app 
10-13 09:13:10.787 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 1
10-13 09:13:10.788 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 2
10-13 09:13:10.791 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 3
10-13 09:13:10.792 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 4
10-13 09:13:10.794 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 5
10-13 09:13:10.795 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 6
10-13 09:13:10.797 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 7
10-13 09:13:10.799 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 8
10-13 09:13:10.800 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 9
10-13 09:13:10.801 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 10
10-13 09:13:10.803 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 11
10-13 09:13:10.804 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 12
10-13 09:13:10.806 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 13
10-13 09:13:10.807 27330-27355/com.example.android.mysunshine.app D/mainTag:  row id value? 14
10-13 09:13:10.825 27330-27355/com.example.android.mysunshine.app D/mainTag: FetchWeatherTask Complete. 14 rows Inserted
10-13 09:13:10.832 27330-27355/com.example.android.mysunshine.app D/mainTag: immediate count: 1
10-13 09:13:10.834 27330-27330/com.example.android.mysunshine.app D/mainTag: row count? 1
10-13 09:13:10.834 27330-27330/com.example.android.mysunshine.app D/mainTag: swapping cursor
10-13 09:13:10.835 27330-27330/com.example.android.mysunshine.app D/mainTag: cursor?: true
10-13 09:13:10.842 27330-27330/com.example.android.mysunshine.app D/mainTag:  HOW MANY ROWS? 1
你知道吗? bulkInsert中的代码显示我已插入14行,但查询只返回一行。 有人可以帮忙吗?

0 个答案:

没有答案