【问题标题】:Android : Issue deleting last item from RecyclerViewAndroid:从 RecyclerView 删除最后一项的问题
【发布时间】:2017-12-31 19:24:03
【问题描述】:

我正在以下列方式使用RecyclerView 来显示在 SQLite DB 中更新的 Places 项目列表。

当我刷它们时,这些项目被删除并更新。 但是当我滑动删除最后一项时,它又回到了视图中。当我关闭应用程序并重新打开应用程序时,该项目被删除。

但是当项目被滑动删除时,在应用程序运行的那个实例中,即使多次滑动,该项目也不会从视图中删除。

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
     int swipedPosition = viewHolder.getAdapterPosition();
     PlaceListAdapter adapter = (PlaceListAdapter) mRecyclerView.getAdapter();

     int pos = viewHolder.getAdapterPosition();
     // Build appropriate uri with String row id appended
     String stringId = places.get(pos).getId();

     Log.d("testhro", stringId);

     Uri uri = PlaceContract.PlaceEntry.CONTENT_URI;
     uri = uri.buildUpon().appendPath(stringId).build();

     Log.d("testhhd", uri.toString());
     // COMPLETED (2) Delete a single row of data using a ContentResolver
     getContentResolver().delete(uri, null, null);
     mAdapter.notifyDataSetChanged();
     refreshPlacesData();

}

refreshPlacesData()

 private void refreshPlacesData() {
        Uri uri = PlaceContract.PlaceEntry.CONTENT_URI;
        Cursor data = getContentResolver().query(
                uri,
                null,
                null,
                null,
                null);

        if (data == null || data.getCount() == 0) return;

        Log.d("countIs", String.valueOf(data.getCount()));

        List<String> guids = new ArrayList<String>();
        while (data.moveToNext()) {
            guids.add(data.getString(data.getColumnIndex(PlaceContract.PlaceEntry.COLUMN_PLACE_ID)));
        }
        PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi.getPlaceById(mClient,
                guids.toArray(new String[guids.size()]));
        placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
            @Override
            public void onResult(@NonNull PlaceBuffer places) {
                mAdapter.swapPlaces(places);
                MainActivity.this.places = places;
                mGeofencing.updateGeofencesList(places);
                if (mIsEnabled) mGeofencing.registerAllGeofences();
            }
        });
    }

我错过了什么吗?谢谢。

编辑: 我尝试更新适配器而不是 mAdapter ,但仍然没有变化

编辑 2

public void swapPlaces(PlaceBuffer newPlaces) {
        mPlaces = newPlaces;
        if (mPlaces != null) {
            // Force the RecyclerView to refresh
            this.notifyDataSetChanged();

        }
    }

注意:我还验证了记录器:

D/countIsZero: true(在 refreshPlacesData() 内)

【问题讨论】:

    标签: java android sqlite android-recyclerview


    【解决方案1】:

    您应该在更改数据集后刷新您的View

       MainActivity.this.places = places; // Data set changed
       PlaceListAdapter adapter = (PlaceListAdapter) mRecyclerView.getAdapter();
       adapter.notifyDataSetChanged();
    

    或者你应该改变

    的顺序
       mAdapter.notifyDataSetChanged(); // Notify later
       refreshPlacesData(); // Refresh first
    

    先更改数据集,然后通知适配器。

    【讨论】:

    • 实际上我也在更新 swapPlaces 方法中的数据集,我会发布它,所以即使我在这里不使用 notifyDataSetChanged(),它仍然以相同的方式工作
    【解决方案2】:

    解决了..

    当我删除最后一项时,数据计数将变为 0,在这种情况下,我当前只是返回。

    因此,以下是我在 refreshPlacesData() 中添加的内容:

      if (data.getCount() == 0) {
                Log.d("countIsZero","true");
                mAdapter.swapPlaces(null);
                return;
            }
    

     public void swapPlaces(PlaceBuffer newPlaces) {
            mPlaces = newPlaces;
    
            if(mPlaces==null)
            {
                Log.d("mPlaceNull","true");
                this.notifyDataSetChanged();
                return;
            }
    

    奇怪的是,这里唯一发生的事情是 this.notifyDataSetChanged(); 在适配器对象上被调用,但如果我以这种方式使用它并跳过对 swapPlaces() 的调用,它就不起作用,即

    if (data.getCount() == 0) {
                Log.d("countIsZero","true");
                mAdapter.notifyDataSetChanged();
                return;
            }
    

    没用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多