fsdf fsd
fsdf fsd

Reputation: 1321

open listview when click button

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

Upvotes: 1

Views: 6680

Answers (3)

Carnal
Carnal

Reputation: 22064

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>

Upvotes: 2

Dev.Sinto
Dev.Sinto

Reputation: 6852

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.

Upvotes: 0

k0nig
k0nig

Reputation: 340

Can use a listActivity

listActivity on android developer

Upvotes: 0

Related Questions