Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9088071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:50:24+00:00 2026-06-16T21:50:24+00:00

Iam trying to implement contacts with a checkbox and selecting multiple contacts and saving

  • 0

Iam trying to implement contacts with a checkbox and selecting multiple contacts and saving them..below is my code

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

            }

but iam getting ArrayIndexOutOfBoundsException at line

 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)

i am struck , Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:50:25+00:00Added an answer on June 16, 2026 at 9:50 pm

    You have to assign values to checkBoxState. After you initialize it here checkBoxState=new boolean[listCont.size()]; it needs values, as in checkBoxState[i] = true;.

    When you are trying to read its values on line viewHolder.checkBox.setChecked(checkBoxState[position]);, it crashes because nothing is assign. The only time you assign any values is during the onClick() which doesn’t occure before you set it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
I am trying to implement web browser using Swing. Here is code below. import
I am trying to implement a gridview with checkbox column on asp.net (VB). when
iam trying to implement an Searchmachine into my site. Iam using Zend_Search_Lucene for this.
I am trying to implement this code in my app to backup data to
I am trying to implement code-first approach of entity framework. I have four entities
I am trying to implement below flow in Spring Integration. Below is my requirement.
I am trying implement a most recent widget into my tumblr post. So far,
I am trying to implement a drag and drop feature. On the left pane
I am trying to implement a to-do-list website to practice using Django. In models.py,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.