Coding for ListView Selections

Android Simple Lists: 1 2 3

In the previous section, you wrote code to display a list of items. However, nothing happens when you click on item on your list. We'll solve that now.

The reason nothing happens when you tap an item on your list is because there's nothing listening out for those taps. Let's add a listener.

In a previous section, you added a listener inside the onCreate method. But you can also add them elsewhere in your code. The listener we need is called OnItemClickListener. This is actually a class that you create a new object for.

Just below your onCreate method, add the following:

private AdapterView.OnItemClickListener listClick = new AdapterView.OnItemClickListener () {

};

We're setting up a new OnItemClickListener that we've called listClick.

You'll probably get red text for AdapterView, so press ALT + ENTER to add the library, or add import android.widget.AdapetrView; to the top of your code.

You'll also have an underline for AdapterView.OnItemClickListener. This is because you'll need to add the following method between the curly brackets of OnItemClickListener:

public void onItemClick(AdapterView parent, View v, int position, long id) {

}

This is a method called onItemClick. As its name suggests, it responds to items being clicked in your list. It has four parameters between its round brackets. The only one of these that we'll make use of is the int position one. It's between the curly brackets of this method that you add code to do something.

But your code should look like this, so far:

Adding a Listener to Android Java code

What we're going to do in our OnItemClick method is to create a second Activity. We'll then send a message to this new Activity, telling it which items was selected.

To get which item was selected, there is a method of the ListView called getItemAtPosition. Add this line to your OnItemClick method:

String itemValue = (String) theListView.getItemAtPosition( position );

We're setting up a String variable called itemValue. After an equal sign, we have the name of our ListView, theListView, followed by getItemAtPosition( position ). The position variable between the round brackets of getItemAtPosition is the same one from between the round brackets of OnItemClick. The position variable tells you which item was selected in your list, starting at item 0. We had Visual Basic .NET as the first item in our list. If this was selected, position would contain a value of zero. If we selected Java from our list, however, the position variable would contain a value of 1. If we selected Android from our list of items (position 2), then getItemAtPosition( position ) would grab the string "Android" and place it into itemValue variable.

The reason we need to grab the String at a particular position is because we want to pass it over to our new Activity.

We haven't created this second Activity yet, though.

Right-click on your app item in the Explorer on the left of Android Studio. From the menus, select new > Activity > Empty Activity:

The New Activity menu in Android Studio

When you click on Empty Activity, you'll see a dialogue box appear. Make sure that Generate Layout File is selected, and that the Activity Name is SecondActivity. Launcher Activity should be unselected, and you want Backwards Compatibility to be checked.

The New Activity dialogue box

Click Finish to add the new layout to your project.

Just like we did in a previous section, we'll add an Intent that references this new class.

Click back on your MainActivity.java tab. Add this line to your onCreate method, just below the setContentView line:

myIntent = new Intent(this, SecondActivity.class);.

This sets up a new Intent, like we did before. We can use this Intent in our OnItemClick method.

Just below your String itemValue line, add this:

myIntent.putExtra("COURSE_SELECTED", itemValue);

We're using putExtra to send information to the new Activity. The key we've come up with is COURSE_SELECTED, and the data we're sending is stored in our itemValue variable.

Finally, for the OnItemClick method, we can start the Activity:

startActivity(myIntent);

As the final line for your onCreate method for the MainActivity.java file, add this line:

theListView.setOnItemClickListener( listClick );

Here, we're calling setOnItemClickListener. This is a method of ListViews. In between the round brackets of setOnItemClickListener we have the name of the Listener we set up, which was listClick.

The whole of you code should look like this:

Creating a new Android Activity from a Listener

We now need to add code for the Second Activity Class. First, click on the activity_second.xml tab. In Design view, drag a TextView onto your blank layout:

A second Activity with a TextView

(You can leave the ID on the default of textView.)

Now click on the SecondActivity.java tab to get at the code. Add the following lines to the onCreate method, just below setContentView:

Intent secondIntent = getIntent( );

String message = "Selected course is " + secondIntent.getStringExtra("COURSE_SELECTED");

TextView myText = (TextView) findViewById(R.id.textView);

myText.setText(message);

This is more or less the same code you wrote for a previous lesson, on Activities. We're just using getIntent( ) to grab the text from the first Activity. We then use the COURSE_SELECTED key to place a message into the TextView.

Your Second Activity code should look like this:

Java code to start a second Android Activity

One final thing left to do. We need a way to get back from the Second Activity to the Main Activity when the app is running. We did this before when we added a line to the Android Manifest file.

Open up your AndroidManifest.xml file, which is in the manifest folder.

Change this line:

<activity android:name=".SecondActivity"></activity>

To this:

<activity android:name=".SecondActivity" android:parentActivityName=".MainActivity" />

You can try it out, now. Run your app to see a list of items:

Android app using a Simple List

Select an item on your list and you'll be taken to your Second Activity:

Seleected list item showing in a new Android Activity

Tap the white arrow at the top to get back to your list.

Try it out in landscape view, as well. If your list of items is too long, scroll down to see more items (in the Emulator, just drag with your left mouse button held down).

Now that you have a good idea of how lists work, we'll create our own Custom Adapter. This will allow us to have a more versatile list than the simple in-built ones. We'll create a list that has an image on the left, as well as text view and sub text view.