Android ListView with Adapter

What is ListView?

  • ListView is a view group that displays a list of scrollable items.
  • ListView is used to show a vertical list of scrollable items, which has data populated using an Adpater.
  • The simplest adapter to use in ListView is called an ArrayAdapter.
  • The main purpose of an ArrayAdapter is to convert an ArrayList of objects into View items loaded into the ListView container.

  • listview arrayadapter

  • In the above diagram, The Adapter (which means ArrayAdapter) fits in between an Data source (ArrayList) and the Adapter View (List View).
The ArrayAdapter configures two aspects:
1. Which array is used as the data source for the list?
2. How to convert any given item in the array into a corresponding View object?

Adapter

  • Adapter manages the data model.
  • It extends the BaseAdapter class.
  • It adopts the data model to the individual entries in the widget.

ListView with ArrayAdapter

  • ArrayAdapter class handles a list or array of Java objects as input.
  • The user can add new elements with the add() method.
  • This class allows to remove all elements in its underlying data structure with the clear() method call.
  • ArrayAdapter creates a view for each array item by calling toString() method and place the contents in a TextView.
Syntax:
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);

Parameters of the above constructor

this – It is the application content.
R.layout – It is a layout defined in an XML file and have TextView for each string in the array.
StringArray – It is an array of strings, populated in the text view.

ListView Attributes

AttributesDescription
android:idIt is the ID of layout for identifying uniquely.
android:entriesIt specifies the reference to an array resource that will populate the ListView.
android:dividerIt is drawable or color to draw between list items.
android:dividerHeightIt specifies the height of the divider.
android:footerDividersEnabledIts default value id true. If it sets to false, the ListView will not draw the divider before each footer view.
android:headerDividersEnabledIts default value is true. If it sets to false, the ListView will not draw the divider after each header view.

Example : ListView Control with ArrayAdapter

XML file 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" />

File Name: MainActivity.java

public class MainActivity extends AppCompatActivity
{
     ListView lvObj;
     String [] city={"Pune","Gwalior","Patna","Nasik","Kashmir",""};
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate(savedInstanceState);       
          setContentView(R.layout.activity_main);   //XML file name activity_main.xml
          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();
               }
          });
     }
}


Output:

listview control

In the above example, the getItemAtPosition() provides the text of selected item from the ListView control.