How to Create a Java Class for a Row Item

Android Custom Lists: 1 2 3 4 5 6 7 8

We've just created an XML file with an image and two text views. The class will be used to get and set the image name, and get and set the headings and sub headings.

To create a new class, right-click your package name in the explorer area on the left, the one just above MainActivity:

Android Studio explorer

From the menu that appears, select New. From the New menu, select Java Class:

The Java Class item on the Android Studio menu

When you click on Java Class, you'll see this dialogue box appear:

The Create New Class dialogue box

Enter RowItem as the Name, and then click OK. You should see a new Java file below your MainActivity:

A new class added to an Android project

If you accidentally close down you RowItem code tab, you can double click here to reopen it.

The code in the RowItem code tab is just a stub, at the moment:

public class RowItem {

}

So, what do we want to go in this class?

Since it's a row item class, we need the name of an image to go in the ImageView; we need some text for the heading; and we need some text for the subheading. We'll also need the name of a big image that we'll use when the ImageView is pressed.

For this class, then, we only need some getters and setters. We can set values in the Main Activity, and get them back out when we create our custom adapter.

Add the following four private field variables to your class:

public class RowItem {

private String heading;
private String subHeading;
private int smallImageName;
private int bigImageName;

}

We have two String variables and two int variables. (The images are int because we have to pass over a drawable to our RowItem class. A drawable needs to be of the int type rather than String.)

Add the following getters and setters for the heading field variable:

public void setHeading( String theHeading ) {

this.heading = theHeading;

}

public String getHeading() {

return this.heading;

}

We're just putting a value in the heading variable with setHeading. We get it back out with getHeading.

Here's the getter and setter for the subheading field variable:

public void setSubHeading( String theSubHeading ) {

this.subHeading = theSubHeading;

}

public String getSubHeading( ) {

return this.subHeading;

}

And here's the getter and setter for the smallImageName field variable:

public void setSmallImageName(int smallName) {

this.smallImageName = smallName;

}

public int getSmallImageName() {

return this.smallImageName;

}

Just one more to go, this time for the bigImageName field variable:

public void setBigImageName(int bigName) {

this.bigImageName = bigName;

}

public int getBigImageName( ) {

return this.bigImageName;

}

The whole of your RowItem Class should look like this (though you'll have a different package name at the top):

Java class code for a ListView row item

OK, we're done with that class. Time to set up a Custom Adapter. We'll do that in the next lesson below.