Android: CheckBox multiple selection ArrayIndexOutOfBoundsEx



我试图实现联系人与复选框和选择多个联系人并保存它们..下面是我的代码

public class ProfileAdapter extends ArrayAdapter<Profile>{
    private String TAG="ContactArrayAdapter";
    private int renderer;
    ArrayList<Profile> myElements;
    HashMap<String, Integer> alphaIndexer;
    String[] sections;
    protected List<Profile> listCont;
    private Activity activity;
    private String profileType;
 // boolean array for storing
    //the state of each CheckBox 
    boolean[] checkBoxState;

    ViewHolder viewHolder;
    public ProfileAdapter(Activity activity, int textViewResourceId, List<Profile> listCont,int renderer, String profileType) {
        super(activity, textViewResourceId, listCont);
        this.renderer = renderer;
        this.listCont = listCont;
        this.activity = activity;
        this.profileType=profileType;
        checkBoxState=new boolean[listCont.size()];
    }
    private class ViewHolder
    {
      ImageView photo;
      TextView text,textContNo, textEmailId;
      CheckBox checkBox;
    }
    @TargetApi(9)
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) (getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE));
            view = inflater.inflate(renderer, null);
             viewHolder=new ViewHolder();
             //cache the views
                viewHolder.photo=(ImageView) view.findViewById(R.id.photo);
                viewHolder.text=(TextView) view.findViewById(R.id.name);
                viewHolder.textContNo=(TextView) view.findViewById(R.id.contactno);
                viewHolder.textEmailId=(TextView) view.findViewById(R.id.emailId);
                viewHolder.checkBox=(CheckBox) view.findViewById(R.id.checkBox1);
                 //link the cached views to the convertview
                view.setTag( viewHolder);
        }
          else
           viewHolder=(ViewHolder) view.getTag();

                   viewHolder.text = (TextView) view.findViewById(R.id.name);
                   viewHolder.photo = (ImageView) view.findViewById(R.id.photo);
                   viewHolder.textContNo = (TextView) view.findViewById(R.id.contactno);
                   viewHolder.textEmailId = (TextView) view.findViewById(R.id.emailId);
        Profile contact = listCont.get(position);
        viewHolder.text.setText(contact.getName());
        contact.getName();
        contact.getId();
        viewHolder.text.setTag(contact);
        viewHolder.text.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Profile myContact= (Profile) v.getTag();
                Intent intent = new Intent();
                intent.putExtra("type", profileType);
                intent.putExtra("name", myContact.getName());
                intent.putExtra("email", myContact.getEmail());
                intent.putExtra("contactid", myContact.getId());
                intent.putExtra("address", myContact.getAddress());
                intent.putExtra("city", myContact.getCity());
                intent.putExtra("state", myContact.getState());
                intent.putExtra("countryName", myContact.getCountryName());
                intent.putExtra("postalCode", myContact.getPostalCode());
                intent.putExtra("website", myContact.getWebSite());
                intent.putExtra("mobileNumber", myContact.getMobileNo());
                intent.putExtra("phoneNumber", myContact.getLandLineNo());
                Log.d(TAG, "On Activity Result Method : 1");
                activity.setResult(100, intent);
                activity.finish();
                Constants.loadEntries.cancel(true);
                return false;
            }
        });

        if(contact.getPhoto() !=null && !contact.getPhoto().equals("")){
            viewHolder.photo.setImageBitmap(contact.getPhoto());
        }else{
            viewHolder.photo.setImageResource(R.drawable.profile);
        }
        viewHolder.photo.setTag(contact);
        viewHolder.photo.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Profile myContact= (Profile)v.getTag();
                Intent intent = new Intent();
                intent.putExtra("type", profileType);
                intent.putExtra("name", myContact.getName());
                intent.putExtra("email", myContact.getEmail());
                intent.putExtra("contactid", myContact.getId());
                intent.putExtra("address", myContact.getAddress());
                intent.putExtra("website", myContact.getWebSite());
                intent.putExtra("mobileNumber", myContact.getMobileNo());
                intent.putExtra("phoneNumber", myContact.getLandLineNo());
                Log.d(TAG, "On Activity Result Method : 2");
                activity.setResult(100, intent);
                activity.finish();
                Constants.loadEntries.cancel(true);
                return false;
            }
        });
        viewHolder.textContNo.setText(contact.getNumber());
        viewHolder.textEmailId.setText(contact.getEmail());

        view.setClickable(true);
        view.setTag(contact);
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Profile myContact= (Profile) v.getTag();
                Intent intent = new Intent();
                intent.putExtra("type", profileType);
                intent.putExtra("name", myContact.getName());
                intent.putExtra("email", myContact.getEmail());
                intent.putExtra("contactid", myContact.getId());
                intent.putExtra("address", myContact.getAddress());
                intent.putExtra("website", myContact.getWebSite());
                intent.putExtra("mobileNumber", myContact.getMobileNo());
                intent.putExtra("phoneNumber", myContact.getLandLineNo());
                Log.d(TAG, "On Activity Result Method : 3");
                activity.setResult(100, intent);
                activity.finish();
                Constants.loadEntries.cancel(true);
            }
        }); 
        LinearLayout layout = (LinearLayout)view.findViewById(R.id.profilelayout);
        layout.setTag(contact);
        layout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Profile myContact= (Profile) v.getTag();
                Intent intent = new Intent();
                intent.putExtra("type", profileType);
                intent.putExtra("name", myContact.getName());
                intent.putExtra("phoneNumber", myContact.getLandLineNo());
                intent.putExtra("email", myContact.getEmail());
                intent.putExtra("contactid", myContact.getId());
                intent.putExtra("address", myContact.getAddress());
                intent.putExtra("website", myContact.getWebSite());
                intent.putExtra("mobileNumber", myContact.getMobileNo());
                Log.d(TAG, "On Activity Result Method : 4");
                activity.setResult(100, intent);
                activity.finish();
                Constants.loadEntries.cancel(true);
            }
        });
        //VITAL PART!!! Set the state of the 
       //CheckBox using the boolean array
            viewHolder.checkBox.setChecked(checkBoxState[position]);

               //for managing the state of the boolean
               //array according to the state of the
               //CheckBox
               viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
        if(((CheckBox)v).isChecked())
         checkBoxState[position]=true;
        else
         checkBoxState[position]=false;
        }
       });

        if (position % 2 == 0) 
            view.setBackgroundResource(R.drawable.listshape); 
            else 
            view.setBackgroundResource(R.drawable.favoritebody); 
            if (contact.getAddress() != null && contact.getAddress().length() != 0){ 
                view.setBackgroundResource(R.drawable.hasaddress); 
            } 
            return view;
            } 
            }

但我得到ArrayIndexOutOfBoundsException在行

 viewHolder.checkBox.setChecked(checkBoxState[position]);
01-02 20:14:51.926: E/AndroidRuntime(26353): java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
01-02 20:14:51.926: E/AndroidRuntime(26353):    at org.app.myhood.adaptor.ProfileAdapter.getView(ProfileAdapter.java:215)
01-02 20:14:51.926: E/AndroidRuntime(26353):    at android.widget.AbsListView.obtainView(AbsListView.java:2468)
01-02 20:14:51.926: E/AndroidRuntime(26353):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
01-02 20:14:51.926: E/AndroidRuntime(26353):    at android.widget.ListView.onMeasure(ListView.java:1162)
01-02 20:14:51.926: E/AndroidRuntime(26353):    at android.view.View.measure(View.java:15481)
01-02 20:14:51.926: E/AndroidRuntime(26353):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)
01-02 20:14:51.926: E/AndroidRuntime(26353):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
01-02 20:14:51.926: E/AndroidRuntime(26353):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)

我很震惊,任何帮助都很感激。

您必须为checkBoxState分配值。在这里初始化checkBoxState=new boolean[listCont.size()];之后,它需要值,如checkBoxState[i] = true;

当您试图在viewHolder.checkBox.setChecked(checkBoxState[position]);上读取其值时,它崩溃了,因为没有分配任何值。你唯一一次分配任何值是在onClick()期间,在你设置它之前不会发生。

检查这一行

    checkBoxState=new boolean[listCont.size()];

当错误发生时,你正在访问一个不存在的索引。

在访问数组之前也设置一个断点来检查元素的数量。

最新更新