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 7939913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:14:11+00:00 2026-06-03T23:14:11+00:00

I’ve been having a lot of trouble with this problem. I have a listview

  • 0

I’ve been having a lot of trouble with this problem. I have a listview that contains:

ImageView / contactName / TextView / CheckBox

The contactName in the listview is populated by reading the contacts on the phone from a SimpleCursorAdapter. All for elements show when the app runs, but the problem I’m having is connecting the checkboxes to their corresponding item in the list.

Through some research, I found that I must use a getView() to link the checkboxes with the items in the list, but through practice, I can’t seem to get it to work right. Furthermore, none of the examples I’ve tried really explained how to apply getView(). The most full example I’ve been working from is from here:

http://androidcocktail.blogspot.com/2012/04/adding-checkboxes-to-custom-listview-in.html

The twist is that this reads and populates my listview with my contacts:

private void populateContactList() {
    // Build adapter with contact entries
    Cursor cursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME       
    };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
            fields, new int[] {R.id.contactEntryText});    
    lv.setAdapter(adapter);        
} // END POPULATECONTACTLIST


private Cursor getContacts()
{ 
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME
    };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
            (chkboxAllVisible ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
} // END GETCONTACTS 

How do I link each checkbox to the a corresponding contact items in my listview?

  • 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-03T23:14:12+00:00Added an answer on June 3, 2026 at 11:14 pm

    Ok i have created a test project for you try to understand code if any problem you are having then ask I will try to help you…

    HERE IS MY ONCREATE FUNCTION OF ACTIVITY.

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ArrayList<String> elements = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
        elements.add("elements " + i);
    }
    
    CheckBox master_cb = new CheckBox(getApplicationContext());
    master_cb.setText("Check All");
    //HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
    ListView lv = (ListView) findViewById(R.id.listView1);
    //HERE I AM CREATING CUSTOM ADAPTER OBJECT.
    my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
    lv.addHeaderView(master_cb);
    lv.setAdapter(adapter);
    master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Intent my_intent = new Intent("master_check_change");
            my_intent.putExtra("check_value", isChecked);
            sendBroadcast(my_intent);
        }
    });
    

    }

    HERE IS MY CUSTOM ADAPTER.

    public class my_custom_adapter extends ArrayAdapter<String> {
        private Context context                     = null;
        ArrayList<String>  elements                 = null;
        private ArrayList<Boolean> itemChecked      = null;
    
        public my_custom_adapter(Context context, int type, ArrayList<String>  elements)
        {
            super(context, type, elements);
            this.elements =  elements;
            this.context = context;
            itemChecked = new ArrayList<Boolean>();
            BroadcastReceiver receiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals("master_check_change")) {
                        boolean check_value = intent.getBooleanExtra("check_value", false);
                        set_checked(check_value);
                        notifyDataSetChanged();
                    }
                }
            };
            context.registerReceiver(receiver, new IntentFilter("master_check_change"));
            set_checked(false);
        }
    
        // AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
        private void set_checked(boolean is_checked)
        {
            for (int i=0; i < elements.size(); i++) {
                itemChecked.add(i, is_checked);
            }
        }
    
        //THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
        static class ViewHolder
        {
            public TextView tv;
            public CheckBox cb;
            public ImageView iv;
        }
    
        @Override
        public View getView (final int position, View convertView, ViewGroup parent)
        {
            View rowView = convertView;
            ViewHolder holder = null;
    
            if (rowView == null) {
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                                   Context.LAYOUT_INFLATER_SERVICE);
                // HERE I AM INFLATING LISTVIEW LAYOUT.
                rowView = inflater.inflate(R.layout.inflated_layout, null, false);
                holder = new ViewHolder();
                holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
                holder.tv = (TextView) rowView.findViewById(R.id.textView1);
                holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
                rowView.setTag(holder);
    
            } else {
                holder = (ViewHolder) rowView.getTag();
            }
    
            if (holder != null) {
                holder.tv.setText(elements.get(position));
    
                holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                                                 boolean isChecked) {
                        itemChecked.set(position, isChecked);
                    }
                });
    
                if(position < itemChecked.size()) {
                    holder.cb.setChecked(itemChecked.get(position));
                }
            }
            return rowView;
        }
    }
    

    main.xml file is this:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
    
        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
    
        </ListView>
    
    </RelativeLayout>
    

    inflated_layout code is :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="17dp" />
    
    
    
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/checkBox1"
            android:layout_toRightOf="@+id/imageView1"
            android:singleLine="true"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher" />
    
    </RelativeLayout>
    

    if you want to use string array instead of arraylist then replace

        String[] elements = new String[10];
        for (int i = 0; i < 10; i++) {
            elements[i] = "elements " + i;
        }
    

    // IN YOUR CUSTOM ADAPTER CUNSTRUCTOR

    public my_custom_adapter(Context context, int type, String[]  elements)
    

    and some more changes accordingly

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.