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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:48:07+00:00 2026-06-03T15:48:07+00:00

I have a custom adapter. Within my getView() , I’m trying to populate holder.contact

  • 0

I have a custom adapter. Within my getView(), I’m trying to populate “holder.contact” with a list of my contacts, but with no success. I do have this method of populating my ListView with contacts, but it disrupts the functionality of my CheckBoxes. This code is outside of my CustomAdapter class:

public 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

public 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 + " = '" +
            (false ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

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

But I’m trying to define it within here, but with no success (Custom Adapter class):

@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.contact_entry, null, false);
        holder = new ViewHolder();
        holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
        holder.contact = (TextView) rowView.findViewById(R.id.contactEntryText);
        holder.tv = (TextView) rowView.findViewById(R.id.SMSCounttext);
        holder.cb = (CheckBox) rowView.findViewById(R.id.cb_contactHistoryChk);


        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();
    }

Is there a way to apply my populateContactList() method within the getView()?

My constructor looks like this. “elements” is already used by another textview:

public CustomAdapter(Context context, int type, ArrayList<String> elements){
    super(context, type, elements);
  • 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-03T15:48:10+00:00Added an answer on June 3, 2026 at 3:48 pm

    Your question is a little ambiguous. You have the populateContactList() which binds a Cursor with Contacts data to a SimpleCursorAdapter and you want to use this method in the getView() method of another custom Adapter(if this is the case, don’t do this)? Or you want to use your own adapter and bind to it the data with the contacts(if this is the case, then use the results from the query, cursor, with your own Adapter)?

    You don’t say what type of Adapter your custom Adapter extends. If it is a Cursor based Adapter, things are easy and you can use the bindView and newView to access the cursor data(and you pass the Cursor from getContacts() in the constructor(to the super class)). Otherwise put the data from the Cursor you get with getContacts() in a structure like an ArrayList and use that as the data for the adapter. (I would recommend to go with the first option).

    Also, it’s your job to maintain the status of CheckBoxes from your row layout(there are many questions here on how to do this).

    Edit:

        ArrayList<String> names = new ArrayList<String>();
        Cursor cursor = getContacts();
        while (cursor.moveToNext()) {
             names.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME )));
    
    }
    

    Edit:
    Use the elements ArrayList and bind it to the rows:

        //...
        private ArrayList<String> data;
    
        public CustomAdapter(Context context, int type, ArrayList<String> elements){
            super(context, type, elements);
            this.data = elements;
        }
    
        public int getCount() {
            return data.size();  
        }
    
        @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.contact_entry, parent, false); //<= use parent instead of null
                holder = new ViewHolder();
                holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
                holder.contact = (TextView) rowView.findViewById(R.id.contactEntryText);
                holder.tv = (TextView) rowView.findViewById(R.id.SMSCounttext);
                holder.cb = (CheckBox) rowView.findViewById(R.id.cb_contactHistoryChk);
                rowView.setTag(holder);
            } else {
                holder = (ViewHolder) rowView.getTag();
            }
            //bind the data to the row views
            String contactName = data.get(position);
            holder.contact.setText(contactName);
            // other stuff you might do to build thw row
    //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview connected to a custom array adapter. This list shows information
I have created a custom Base Adapter class, to populate a list view with
I have a ListView with a custom adapter, and an animation in getView() for
I have a custom list adapter: class ResultsListAdapter extends ArrayAdapter<RecordItem> { in the overridden
I am ultimately trying to unit test a custom base adapter, but I am
I have a custom adapter extended from the SimpleCursorAdapter. Using this I'm binding a
I have a custom cursor adapter and I am trying to set the color
I have a custom adapter that visualize each row in the list of Orders.
I have ListView with custom Adapter which supplies View to ListView in this way:
I have a custom list adapter contains a layout with controls in it. I

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.