Android Listview With Adapter

Explain ListView control with ArrayAdapter.

Drag the ListView control on your UI from the widgets. Following will be the XML code for ListView control.
<ListView
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/listView"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true" />

Following is the content of the modified MainActivity.java file
public class MainActivityextends AppCompatActivity
{
     ListViewlvObj;
     String [] city={"Pune","Gwalior","Patna","Nasik","Kashmir",""};
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          lvObj = (ListView)findViewById(R.id.listView);
          ArrayAdapter<String>arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,city);
          lvObj.setAdapter(arrayAdapter);
          lvObj.setOnItemClickListener(new AdapterView.OnItemClickListener()
          {
               @Override
               public void onItemClick (AdapterView<?> parent, View view, intposition, long id)
               {
                    String item = (String)lvObj.getItemAtPosition(position);
                    Toast toast = Toast.makeText(getApplicationContext(),"Your selected city ="+item,Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER,30,150);
                    toast.show();
               }
          });
     }
}

List view demo

Listview has method named getItemAtPosition(). By using this method, you can get the text of selected item from the listview control.