gpt4 book ai didi

java - 安卓 : Issue deleting last item from RecyclerView

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:19 24 4
gpt4 key购买 nike

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

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

但是当项目被滑动删除时,在应用程序运行的情况下,即使多次滑动,该项目也不会从 View 中删除。

@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()内)

最佳答案

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

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

或者你应该改变

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

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

关于java - 安卓 : Issue deleting last item from RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45310556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com