1

i want to do something that when i click on button he will openfor me something like listview with the name of all the contacts in my phone...how can i do it? i know how to get the names of all the contacts in my phone and put them into string array,however how can i open a new windows with a listview of all the contacts name when i click on button?

thanks

0

3 Answers 3

2

In your first Activity when you click on button:

startActivity(new Intent(this, ContactsActivity.class));

Then in your ContactsActivity:

public class ContactsActivity extends ListActivity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.contacts_view);

            ListAdapter adapter = createAdapter();
            setListAdapter(adapter);
        }

        /**
         * Creates and returns a list adapter for the current list activity
         * @return
         */
        protected ListAdapter createAdapter()
        {
            // List with strings of contacts name
            contactList = ... someMethod to get your list ...

            // Create a simple array adapter (of type string) with the test values
            ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactList);

            return adapter;
        }
    }

XML file for your ContactsActivity (name it to contacts_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"
     >
     <ListView  
         android:id="@android:id/list"
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         />
     <TextView android:id="@android:id/empty"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Empty set"
         />
 </LinearLayout>
Sign up to request clarification or add additional context in comments.

5 Comments

thanks! and what is the xml file that i need to create? and from the main_activity in i need to write: "new ContactsActivity" in order to open the listactivity window when i click on the button?
ok,thank i understand it..and as i understand from your code that i need to create an xml file called : "simple_list_item_1"..what do i need to implement in this xml file? thanks in advance
ohh no, that simple_list_item_1 is a built in xml file from android :)
when you see "android.R.layout" before the xml name, it means it's the android's xml files.
I have inserted now the full code for you, just copy and paste it. Then in your res/layout create this file called "contacts_view.xml" and paste XML code in there.
0

Can use a listActivity

listActivity on android developer

2 Comments

thanks,can you give me an example how can i do it programmatically?thanks
In the link above you have a small example, in higherpass.com/Android/Tutorials/… is the same (think) but explained better
0

On the button click you need to start new Activity(or ListActivity) that contains list of all contacts.So you need to set onClicklistener then in onClick function you need to write code for starting Activity

On second activity Create a CustomAdapter that will initialised with say List

class Contact
{
    private String number;
    private String name
}

Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.