适配器的onBindView给出错误:java.lang.IndexOutOfBoundsException:索引:0,大小:0

时间:2018-08-10 13:25:47

标签: android

由于某些原因,当我尝试运行代码时,它会弹出 java.lang.IndexOutOfBoundsException:索引:0,大小:0                                                                  在java.util.ArrayList.get(ArrayList.java:411)                                                                  在krispo.callie.ContactAdapter.onBindViewHolder(ContactAdapter.java:96)                                                                  在krispo.callie.ContactAdapter.onBindViewHolder(ContactAdapter.java:27)

有人可以解释一下这是什么意思,我该如何解决这个问题?我一直在寻找答案,但没有一个有效。

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

    }







}

2 个答案:

答案 0 :(得分:3)

删除此行

#background-video { width: 640px; height: 360px; position: relative; }
#background-video iframe { width: 100%; height: 100%; display: none; }
#background-video .play-btn { position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; font-size: 0px; line-height: 0px; background: url(http://www.legacycitychurch.com/media/cover_art/Play%20Button%20Overlay/playbutton.png) center center no-repeat; background-size: 120px auto; }

答案 1 :(得分:1)

删除这些行:

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

每次创建新项目时,您都会删除有关列表的所有信息。