Home and Learn: Android Course
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:

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

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

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

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):

OK, we're done with that class. Time to set up a Custom Adapter. We'll do that in the next lesson below.
< Custom Row Layout| Custom Adapter Class >
Back to the Android Contents Page
Email us: enquiry at homeandlearn.co.uk