20 June 2014

Making a simple List in Android .

Most Applications contain list as a major menu listing layout .

What do you need to make a list ?
  1. A Linear Layout
  2. A ListView
  3. String Array
  4. Array Adapter
  5. Toast
What we are going to do ?
We are going to make a simple list with some car names  and displaythe names of the car selected in a Toast (As I love cars, you can choose your own topic of interest)

Step 1 : Drag a ListView from Composite field in the Palette to the Linear Layout
drag listview

Step 2 : The code you'll find when you click on activity_main.xml will be :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" >
        android:layout_height="wrap_content" >
You might see a <ListView> tag occuring in the code . It has an id attribute. change it since you have to remember it further . The default might be listView1. I have changed it to simplelist  so I can refer it easily later...

Step 3 : The code for the Java file MainActivity.java in src directory is :
package com.mia.listproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

    ListView simplelist;
    protected void onCreate(Bundle savedInstanceState) {
        simplelist = (ListView) findViewById(R.id.list);
          String[] values = new String[] { "Audi", "BMW", "Mercedes", "RollsRoyce" };
          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);
           simplelist.setOnItemClickListener(new OnItemClickListener()    

            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {                   
                // TODO Auto-generated method stub\
                   // ListView Clicked item index
                   int itemPosition     = position;
                   // ListView Clicked item value
                   String  itemValue    = (String) simplelist.getItemAtPosition(position);
                    // Show Alert 
                      "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)

Step 4 : Run the project .
When you click on Audi you will get position : 0 and ListItem : Audi
position and list item

When you click on RollsRoyce you will get Position : 4 and ListItem : 3
position and list item

Explanation of the code :
  • First we have to make an reference of a predefined class ListView so that we can use it further. For that we import ListView class form widget package.
ListView simplelist;
  • We connect the reference simplelist to the ListView in the xml layout by giving the id as the parameter for the function findViewById (Remember the id (bold in the xml code) )
simplelist = (ListView) findViewById(R.id.list);
  • Now we make an array of Strings named values which contains the name of our list-items .
String[] values = new String[] { "Audi", "BMW", "Mercedes", "RollsRoyce" };
  • We define an object of class ArrayAdapter which is a container for String array. It will tell the compiler that the String values are nothing but the elements of the defined list .
ArrayAdapter<String> adapter = new 
ArrayAdapter<String(this, android.R.layout.simple_list_item_1, 
android.R.id.text1, values);
  • It takes parameters such as the type of layout we have used(simple_list_item). Since the values in the list are text so text1 and the String array name which is values .
  • Then we have to set the adapter by passing object of ArrayAdapter as a parameter to setAdapter function .
  • Then we have to use onItemClick function which defines what should be done when we click the list-item.
simplelist.setOnItemClickListener(new OnItemClickListener()    

            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {
  • Then we define an integer which takes the position of list-item from the method parameter named position stores the position of list-item in it.
int itemPosition=position;
  • Then we define a String itemValue which defines the value (i.e. BMW or Mercedes ) from the list-item . Specific itemValue is known when we pass the related position to function getItemAtPosition
String itemValue = (String) simplelist.getItemAtPosition(position);
  • As the Item in list is clicked we will get a Toast showing the position the value at the position.
Stay Tuned with Made In Android 

Previous Page Next Page Home