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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:28:08+00:00 2026-05-20T14:28:08+00:00

I have modified this example from the SDK pages to grab all the Contact

  • 0

I have modified this example from the SDK pages to grab all the Contact Groups from the phone and display them. I cannot, however, figure out how to click one of them and then use the Groups._ID to do something else. Can someone teach me how to get a click/select listener on this list?

MyListAdapter.java

package com.example.simplelist;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.Groups;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class MyListAdapter extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_list_activity_view);
        Cursor mCursor = this.getContentResolver().query(Groups.CONTENT_URI, null, null, null, null);
        startManagingCursor(mCursor);
        ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, mCursor, new String[] {Groups.NAME, Groups._ID}, new int[] {android.R.id.text1, android.R.id.text2});
        setListAdapter(adapter);
    }
}

custom_list_activity_view.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:paddingLeft="8dp"
         android:paddingRight="8dp">

     <ListView android:id="@id/android:list"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false" android:choiceMode="singleChoice" android:clickable="true" />

     <TextView android:id="@id/android:empty"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:background="#FF0000"
               android:text="No data"/>
 </LinearLayout>

two_line_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical">

     <TextView android:id="@+id/text1"
         android:textSize="16sp"
         android:textStyle="bold"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>

     <TextView android:id="@+id/text2"
         android:textSize="16sp"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>
 </LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.simplelist"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="3" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MyListAdapter"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>
  • 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-05-20T14:28:08+00:00Added an answer on May 20, 2026 at 2:28 pm

    This should allow you to interact with selected item the way you want.

    MyListAdapter.java

    package com.example.simplelist;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import android.app.ListActivity;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.Contacts.Groups;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MyListAdapter extends ListActivity {
    
        List<ContactGroup> groups = new ArrayList<ContactGroup>(); 
    
        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            this.groups = getContacts();
            ContactGroupAdapter cAdapter = new ContactGroupAdapter(this); 
            setListAdapter(cAdapter);
        }
    
        private List<ContactGroup> getContacts(){
            List<ContactGroup> grps = new ArrayList<ContactGroup>();
            ContentResolver cr = getContentResolver();
            Cursor groupCur = cr.query(Groups.CONTENT_URI, null, null, null, null);
            if (groupCur.getCount() > 0) {
                while (groupCur.moveToNext()) {
                    ContactGroup newGroup = new ContactGroup();
                    newGroup.Id = groupCur.getString(groupCur.getColumnIndex(Groups._ID));
                    newGroup.Name = groupCur.getString(groupCur.getColumnIndex(Groups.NAME));
                    grps.add(newGroup);
                }
            }
            return grps;
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            ContactGroup selectedGroup = new ContactGroup();
            selectedGroup = groups.get(position);
            Toast.makeText(getBaseContext(), selectedGroup.Name + " ID #" + selectedGroup.Id, 1).show();
        }
    
    
        public class ContactGroupAdapter extends BaseAdapter{
            public ContactGroupAdapter(Context c) {
                mContext = c;
            }
            @Override
            public int getCount() {
                return groups.size();
            }
    
            @Override
            public Object getItem(int position) {
                return position;
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder;
                if(convertView == null){
                    LayoutInflater vi = LayoutInflater.from(this.mContext);  
                    convertView = vi.inflate(R.layout.two_line_list_item, null);
                    holder = new ViewHolder();
                    convertView.setTag(holder); 
                }
                else {
                    //Get view holder back
                    holder = (ViewHolder) convertView.getTag();
                }
                ContactGroup c = groups.get(position);
                if (c != null) {
                    //Name
                    holder.toptext = (TextView) convertView.findViewById(R.id.text1);
                    holder.toptext.setText(c.Name);
                    //ID
                    holder.bottomtext = (TextView) convertView.findViewById(R.id.text2);
                    holder.bottomtext.setText(c.Id);
                }
                return convertView;
            }
            private Context mContext;
    
        }
        public static class ViewHolder {
            TextView toptext;
            TextView bottomtext;
        }
    
        public class ContactGroup{
            public String Id;
            public String Name;
        }
    
    }
    

    two_line_list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" 
        android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    
             <TextView android:id="@+id/text1"
             android:textSize="16sp"
             android:textStyle="bold"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
    
         <TextView android:id="@+id/text2"
             android:textSize="16sp"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"/>
    
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a unit test (example is modified Test::Unit documentation ) require 'test/unit' class
I have modified the example found here to use two io channels. None of
I have a controller in MVC serving up images from a database. EDIT: This
I have found this example on StackOverflow: var people = new List<Person> { new
I have a dataset that I have modified into an xml document and then
I have an XML object (loaded using XMLHTTPRequest 's responseXML ). I have modified
I have a vs.net project, and after some refactoring, have modified the name of
I want to commit a large amount of XML files which have been modified.
If I have added/removed/modified a large number of files in my local ClearCase view,
I have a Map which is to be modified by several threads concurrently. There

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.