0

我正在尝试使用复选框实现联系人并选择多个联系人并保存它们..下面是我的代码

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)

我很震惊,任何帮助表示赞赏。

4

2 回答 2

0

检查这一行

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

稍后发生错误时,您正在访问一个不存在的索引。

在访问数组之前放置一个断点以检查元素的数量。

于 2013-01-02T14:56:03.150 回答
0

您必须将值分配给checkBoxState. 在此处对其进行初始化后,checkBoxState=new boolean[listCont.size()];它需要值,如checkBoxState[i] = true;.

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

于 2013-01-02T15:15:33.453 回答