sixcookies
sixcookies

Reputation: 397

Adapter's onBindView gives error: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

for some reason when I try to run my code, it pops up with java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.get(ArrayList.java:411) at krispo.callie.ContactAdapter.onBindViewHolder(ContactAdapter.java:96) at krispo.callie.ContactAdapter.onBindViewHolder(ContactAdapter.java:27)

Could someone explain what any of this means and how I could solve the problem? I've searched for answers but none have worked.

package krispo.callie;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 21poonkw1 on 9/8/2018.
 */

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.MyViewHolder> {

    private List<Contact> contactsList;
    private static List<Contact> mSelectedItemsIds;
    private int rowLayout;




    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name, number;
        public CheckBox checkBox;
        public MyViewHolder(View view) {
            super(view);

            if(rowLayout == 1) {
                name = (TextView) view.findViewById(R.id.name);
                number = (TextView) view.findViewById(R.id.number);
                checkBox = (CheckBox) view.findViewById(R.id.checkBox);
            }

            mSelectedItemsIds = new ArrayList<>();
            contactsList = new ArrayList<>();



        }
    }


    public ContactAdapter(List<Contact> contactsList,int rowLayout) {
        this.contactsList = contactsList;
        this.rowLayout = rowLayout;
    }


    @Override
    public int getItemCount(){

        return contactsList.size();
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView;

        if(rowLayout == 1) {
            itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.contact_row, parent, false);
        }
        else{
            itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.contact_card, parent, false);
        }

        return new MyViewHolder(itemView);
    }

    public List<Contact> getSelectedIds() {

        return mSelectedItemsIds;
    }

    @Override

    public void onBindViewHolder(final MyViewHolder holder, final int position) {

        final Contact contact = contactsList.get(position);
        holder.name.setText(contact.getName());
        holder.number.setText(contact.getPhoneNumber());

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                Contact currentContact = contactsList.get(position);
                if (isChecked) {
                    mSelectedItemsIds.add(currentContact);
                } else {
                    mSelectedItemsIds.remove(mSelectedItemsIds.indexOf(currentContact));
                    notifyItemRangeChanged(position, getItemCount());
                }
            }
        });

    }







}

Upvotes: 1

Views: 1810

Answers (2)

Drashti Joshi
Drashti Joshi

Reputation: 376

Remove this line

mSelectedItemsIds = new ArrayList<>();

contactsList = new ArrayList<>();

Upvotes: 3

UgAr0FF
UgAr0FF

Reputation: 825

Delete these lines:

mSelectedItemsIds = new ArrayList<>();
contactsList = new ArrayList<>();

Each time a new item is created, you delete all the information about the list.

Upvotes: 1

Related Questions