Jarin Finmomenta
Jarin Finmomenta

Reputation: 25

I am getting issue while deleting an item from RecyclerView

When I click on the close button which I am having inside my recycler item the app gets crashes and throwing error. My code is below:

public void remove(int position) {
    asset_type.remove(position);
    asset_count.remove(position);
    asset_value.remove(position);
    AssetDetailsActivity.assetValueId.remove(position);
    AssetDetailsActivity.assetCountId.remove(position);
    AssetDetailsActivity.assetTypeId.remove(position);
    notifyItemRemoved(position);

}

My error log and I am having more than 2 items in my recycler view

2019-02-25 11:04:16.733 5036-5036/com.tachyloans E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.tachyloans, PID: 5036
    java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
        at java.util.ArrayList.remove(ArrayList.java:503)
        at com.tachyloans.common.MyAdapter.remove(MyAdapter.java:62)
        at com.tachyloans.common.MyAdapter$2.onClick(MyAdapter.java:113)
        at android.view.View.performClick(View.java:6310)
        at android.view.View$PerformClick.run(View.java:24970)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

Upvotes: 0

Views: 76

Answers (1)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

It is because you're trying to delete some items with List.remove(position) sequentially. You're probably deleting the same item position with your following code:

asset_type.remove(position);
asset_count.remove(position);
asset_value.remove(position);
AssetDetailsActivity.assetValueId.remove(position);
AssetDetailsActivity.assetCountId.remove(position);
AssetDetailsActivity.assetTypeId.remove(position);

The source of the problem is usually because you forget that a non-primitive data type is always passed by reference, for example: a List.

In the following typical RecylerView adapter the List (List<YourDatum> data) is passed by reference:

public class YourAdapter extends
    RecyclerView.Adapter<YourAdapter.ViewHolder> {

    ..

    private List<YourDatum> mData;

    public YourAdapter(List<YourDatum> data) {
        mData = data;
    }
}

and that we usually pass the List via the constructor with:

public class YourActivity extends AppCompatActivity {

     ArrayList<YourDatum> mData;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         // ...

         // assuming that createInitializeData(10) will create 10 data
         mData = createInitializeData(10);

         // Now we're passing the data to adapter.
         YourAdapter adapter = new YourAdapter(mData);
         ...
     }
}

So, both the YourActivity.mData and YourAdapter.mData are pointing to the same location. Hence, whenever you're removing the item from YourActivity.mData then YourAdapter.mData item is also removed.

Now, you only need to use the following code for your case:

asset_type.remove(position);
asset_count.remove(position);
asset_value.remove(position);

// the following is not needed because it's incorrect
// AssetDetailsActivity.assetValueId.remove(position);
// AssetDetailsActivity.assetCountId.remove(position);
// AssetDetailsActivity.assetTypeId.remove(position)

Upvotes: 1

Related Questions