2017-07-25 7 views
2

RecyclerViewを次のように使用して、SQLite DBに更新されたプレイスアイテムのリストを表示しています。Android:RecyclerViewの最後のアイテムを削除する問題

アイテムは細かく削除され、スワイプすると更新されます。 最後のアイテムを削除するためにスワイプすると、ビューに戻ります。私は、アプリケーションを閉じて、アプリを再び開くと、アイテムが削除されます。

ただし、アイテムをスワイプして削除すると、そのアプリの実行中に複数回スワイプしても表示されなくなります。

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

私は何かが足りないのですか?ありがとう。

EDIT: 私が代わりにmAdapterのアダプタを更新しようとしたが、まだ何の変化

EDIT 2

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

     } 
    } 

注:私もロガーを検証:

D /しませんcountIsZero:true(refreshPlacesData()内)

答えて

0

データセットを変更した後にViewを更新する必要があります。

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

か、まずデータセットを変更し、アダプタを通知

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

の順序を変更する必要があります。解決

+0

は、アダプタが、無ヘルプを更新してみました:/ – user3820753

+0

[OK]を私はそれが – user3820753

+0

を試してみましょう私がここでnotifyDataSetChanged()を使用しなくても、同じ方法で動作します – Rahul

0

..

私は最後の項目を削除されたように、データ数は、私は現在、ちょうど戻った場合には、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; 
     } 

は機能しません。

関連する問題