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。ここで初期化した後checkBoxState=new boolean[listCont.size()];、 のように値が必要ですcheckBoxState[i] = true;

行でその値を読み取ろうとするとviewHolder.checkBox.setChecked(checkBoxState[position]);、何も割り当てられていないためクラッシュします。onClick()値を割り当てるのは、設定する前に発生しない唯一の時間です。

于 2013-01-02T15:15:33.453 に答える
0

この行を確認してください

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

後でエラーが発生した場所で、存在しないインデックスにアクセスしています。

配列にアクセスする前にもブレークポイントを配置して、要素の数を確認します。

于 2013-01-02T14:56:03.150 に答える