-1

I want show a ListView that will contain a list of cities.When the user click of the edit text.I want to open a dialog in that i want to show a ListView.As the user select the city i want to set that as the value of the edit text.And after selecting the value from the edit if the user again touches the edit again i want to open the dialog and focus will be on the previous value that the user have selected .How can we do this please help me i am new in android. This is my activity file XML

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select City"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout>
Developer
  • 6,292
  • 19
  • 55
  • 115
Pooja Dubey
  • 683
  • 2
  • 14
  • 34

1 Answers1

3

Create A Layout cities_listview

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <ListView
             android:id="@+id/list"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >

         </ListView>

    </LinearLayout>

On EditText OnClickListner

public void searchCitiesList() {
        final Dialog dialog = new Dialog(HotelSearch.this);
        dialog.setContentView(R.layout.cities_listview);
        dialog.setTitle("Select City");
        listView = (ListView) dialog.findViewById(R.id.list);
        String[] values = new String[] { "Delhi",
                "Banglore", "Chennai",
                "Luckhow", "Goa",
                "Pune", "Agra",
                "Dehradun" };
        dialog.show();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                int itemPosition = position;
                String itemValue = (String) listView
                        .getItemAtPosition(position);

                // Show Alert
                Toast.makeText(
                        getApplicationContext(),
                        "Position :" + itemPosition + "  ListItem : "
                                + itemValue, Toast.LENGTH_LONG).show();
                dialog.cancel();

            }

        });
Developer
  • 6,292
  • 19
  • 55
  • 115