Android – Using a list view to show data using the standard array adapter

Android – Using a list view to show data using the standard array adapter

In this article I will describe how to use a ListView to display data using the standard ArrayAdapter. The adapter will receive temperature data and will show it as a simple string in the list. For the data we create our own data class (TemperatureData).

Ok, let’s start with creating the main layout. In this layout we have a TextView which holds a header text and a ListView which will hold the temperature data.

main.xml:
[xml highlight=”16,17-20″]

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:id=”@+id/header”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:gravity=”center_vertical|center_horizontal”
android:textSize=”20sp”
android:textColor=”#FF0000″
android:text=”ListView using standard ArrayAdapter and simple list layout”
/>
<ListView
android:id=”@+id/list”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>
</LinearLayout>

[/xml]

The following code shows the data class which will hold the temperature data. Important in this code is the implementation of the toString() method. Without this method the ArrayAdapter cannot display the list items.

TemperatureData.java:
[java highlight=”23,24-32″]

package nl.jestersoft.listviewtryout;

import android.text.format.Time;

public class TemperatureData
{
// The data
String SensorName;
double CurrentTemperature;
double MinimumTemperature;
double MaximumTemperature;
String TimeStamp;

public TemperatureData(String sensorName, double currentTemperature, double minimumTemperature, double maximumTemperature, String timeStamp )
{
SensorName = sensorName;
CurrentTemperature = currentTemperature;
MinimumTemperature = minimumTemperature;
MaximumTemperature = maximumTemperature;
TimeStamp = timeStamp;
}

@Override
public String toString()
{
String item;

// Just out out the data as a string
item = SensorName + “: ” + CurrentTemperature + “, ” + MinimumTemperature + “, ” + MaximumTemperature + ” (” + TimeStamp + “)”;

return item;
}
}
[/java]

Now we have the main screen and the data definition, let’s take a look at the main code.

ListViewTryout.java:

[java highlight=”25,26,27″]
package nl.jestersoft.listviewtryout;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;

public class ListViewTryout extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Add some dummy data to the list
ArrayList<TemperatureData> tempData = GetDummyData();

// Get list to view data in
ListView list = (ListView)findViewById(R.id.list);

// Show data in the list
int layoutID = android.R.layout.simple_list_item_1;
ArrayAdapter tempAdapter = new ArrayAdapter<TemperatureData>(this, layoutID, tempData);
list.setAdapter(tempAdapter);
}

public ArrayList<TemperatureData> GetDummyData()
{
ArrayList<TemperatureData> dummyData = new ArrayList<TemperatureData>();

TemperatureData data1 = new TemperatureData(“Balcony”, 18.0, 16.0, 20.0, “30-07-2011 13:14”);
dummyData.add(data1);

TemperatureData data2 = new TemperatureData(“Living room”, 24.0, 20.0, 25.0, “30-07-2011 13:14”);
dummyData.add(data2);

TemperatureData data3 = new TemperatureData(“Bath room”, 18.0, 21.0, 27.0, “30-07-2011 13:15”);
dummyData.add(data3);

return dummyData;
}

}
[/java]

Note that the code uses the ‘android.R.layout.simple_list_item_1’ layout for displaying the items.

When you run the code you will see the following result.

Comments are closed.