简体   繁体   English

Android:发出从RecyclerView删除最后一个项目的问题

[英]Android : Issue deleting last item from RecyclerView

I am using the RecyclerView in the following manner to show a list of Places items that are updated in SQLite DB . 我以以下方式使用RecyclerView来显示在SQLite DB中更新的Places项的列表。

The items are deleted fine and updated as i swipe them . 当我轻扫它们时,这些项目将被删除并更新。 But when I swipe to delete the last item , it comes back in the view . 但是,当我滑动以删除最后一项时,它会再次出现在视图中。 When i close the app and reopen the app , the item is deleted though . 当我关闭该应用程序并重新打开该应用程序时,该项目将被删除。

But when item is swiped to delete , in that instance of app running , the item is not deleted from view even after swiping multiple times . 但是,当滑动项目以将其删除时,在运行该应用程序的情况下,即使多次滑动该项目也不会从视图中删除。

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

Am I missing something? 我想念什么吗? Thanks. 谢谢。

EDIT : i tried updating adapter instead of mAdapter , but still no change 编辑:我试图更新适配器而不是mAdapter,但仍然没有变化

EDIT 2 编辑2

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

        }
    }

Note : I also verified the loggers : 注意:我还验证了记录器:

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

You should refresh your View after changing dataset. 更改数据集后,应刷新View

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

or you should change order of 否则您应该更改的顺序

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

Firstly change dataset and then notify adapter. 首先更改数据集,然后通知适配器。

Solved.. 解决了..

As i was deleting the last item , the data count would become 0 , in which case i was currently just returning. 当我删除最后一项时,数据计数将变为0,在这种情况下,我当前正在返回。

So instead of that , following is my addition inside refreshPlacesData() : 因此,除此以外,下面是我在refreshPlacesData()中添加的内容:

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

and

 public void swapPlaces(PlaceBuffer newPlaces) {
        mPlaces = newPlaces;

        if(mPlaces==null)
        {
            Log.d("mPlaceNull","true");
            this.notifyDataSetChanged();
            return;
        }

Weirdly , only thing happening here is this.notifyDataSetChanged(); 奇怪的是,这里唯一发生的事情是this.notifyDataSetChanged(); being called on adapter object , but if i use it this way and skip calling of swapPlaces() , it does not work ie 在适配器对象上被调用,但是如果我以这种方式使用它并且跳过对swapPlaces()调用,它将无法正常工作,即

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

doesn't work. 不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM