0

This is my code for onBindViewHolder:

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {       
        myHolder = holder;
        text = mDataset.get(keys[position]);            
        myHolder.singleItemTextView.setText(text);

}

And this is my code to add data to the RecyclerView:

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    myDataset = new LinkedHashMap<>();


    mAdapter = new HashMapAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);


    myDataset.put("1", "Hello");
    myDataset.put("2", "World");
    mAdapter.notifyDataSetChanged();

The above code is not working, I get java.lang.ArrayIndexOutOfBoundsException: length=0; index=0. But when I put the data before using setAdapter(mAdapter) everything is working:

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);
    myDataset = new LinkedHashMap<>();

    myDataset.put("1", "Hello");
    myDataset.put("2", "World");

    mAdapter = new HashMapAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);

How can I first set adapter, and then add items without getting this error? I need it because I dynamically add items and at first the dataset is empty so I set an adapter with empty dataset.

pileup
  • 1
  • 2
  • 18
  • 45
  • The `myDataset` is the managed data yet you use `position` to index into some `keys` array. You'll need to show how you manage that array - use this answer as a guide: https://stackoverflow.com/a/5234718/2711811. –  Dec 30 '18 at 02:57

1 Answers1

0

If you know that you are going to encounter a error (or exception in this case), you should use try and catch block to catch the exception. This way you'll be able to continue as normal as your application wouldn't crash due to the exception.

try{
    mAdapter = new HashMapAdapter(myDataset);
    mRecyclerView.setAdapter(mAdapter);
}catch(ArrayIndexOutOfBounds ex){
    Log.e(TAG, "Exception: " + ex);
}

You might be able to tackle the problem from the root. I believe the exception is encountered when you are setting the positions within the adapter class and if the arrayList is empty, it will encounter the ArrayIndexOutOfBounds. As the exception stats java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 which means that you are trying to access position 1 (index=0) when the size/length of the array is 0. You are trying to access something that doesn't exist.

Another way to deal with this is to check the indexes are within the bounds of the array (between 0 and the length of the array minus 1) before accessing the array. For example:

if( index >= 0 && index < array.length){
    //safe to access the array
} else {
   //not safe to access the array as you'll encounter exception
}
Nero
  • 1,058
  • 1
  • 9
  • 25