Android Simple ListView Tutorial

In this tutorial, you will learn how to create a simple ListView in your Android application. The ListView is a view group that displays a list of vertical scrollable items. We will create a ListView that allows users to scroll the list, and on ListView item click will show results on a new activity using an intent method. So lets begin…

Create a new project in Eclipse, select File > New > Android Application Project. Fill in the details and name your project ListViewTutorial.

Application Name : ListViewTutorial

Project Name : ListViewTutorial

Package Name : com.androidbegin.listviewtutorial

Open your MainActivity.java and paste the following code.

MainActivity.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

packagecom.androidbegin.listviewtutorial;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.view.View;

publicclassMainActivityextendsActivity{

// Declare variables

ListView listView;

@Override

publicvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

// Get the view from listview_main.xml

setContentView(R.layout.listview_main);

// Store string resources into an Array

String[]SamsungPhones=newString[]{"Galaxy S","Galaxy S2",

"Galaxy Note","Galaxy Beam","Galaxy Ace Plus","Galaxy S3",

"Galaxy S Advance","Galaxy Wave 3","Galaxy Wave Y",

"Galaxy Nexus","Galaxy W","Galaxy Y","Galaxy Mini",

"Galaxy Gio","Galaxy Wave","Galaxy Wave 2"};

// Locate ListView in listview_main.xml

listView=(ListView)findViewById(R.id.listview);

// Bind array strings into an adapter

ArrayAdapter<String>adapter=newArrayAdapter<String>(this,

android.R.layout.simple_list_item_1,android.R.id.text1,

SamsungPhones);

listView.setAdapter(adapter);

// Capture ListView item click

listView.setOnItemClickListener(newOnItemClickListener(){

publicvoidonItemClick(AdapterView<?>parent,View view,

intposition,longid){

// Capture the click position and set it into a string

Stringphones=(String)listView.getItemAtPosition(position);

// Launch SingleItemView.java using intent

Intenti=newIntent(MainActivity.this,SingleItemView.class);

// Send captured string to SingleItemView.java

i.putExtra("phones",phones);

// Start SingleItemView.java

startActivity(i);

}

});

}

@Override

publicbooleanonCreateOptionsMenu(Menu menu){

getMenuInflater().inflate(R.menu.activity_list_view,menu);

returntrue;

}

}

Strings are stored in a string array and set into a ListView using an adapter. We used an onitemclicklistener to capture ListView item click position and pass the selected string to a new activity called SingleItemView.java.

Next, create a new activity in a new class called SingleItemView.java. Go to File > New > Class and name it SingleItemView.java. Select your package named com.androidbegin.listviewtutorialand click Finish.

Open your SingleItemView.javaand paste the following code.

SingleItemView.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

packagecom.androidbegin.listviewtutorial;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.widget.TextView;

publicclassSingleItemViewextendsActivity{

// Declare variable

Stringphones;

@Override

publicvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

// Get the view from single_item_view.xml

setContentView(R.layout.single_item_view);

// Locate the TextView in single_item_view.xml

TextView txtPhones=(TextView)findViewById(R.id.text);

// Get string from intent passed from MainActivity.java

Intenti=getIntent();

phones=i.getStringExtra("phones");

// Set the string into TextView

txtPhones.setText(phones);

}

}

An intent is used to retrieve a string from the previous activity and set the string into a TextView.