Selection by ListView and RadioGroup

Screenshot

Screenshot of the app running in the Gingerbread emulator

The code of MainActivity.pas

Note the use of the ArrayAdapter for use with the ListView. You need an array and the layout of a row to pass to its constructor together with the argument "self". This is a good occasion for starting with (tricky) working demonstration code then modifying it to suit your own circumstances. You can compare the code below with the RemObjects C# version.

namespace org.me.android_listview;

interface

uses
  java.util,
  android.app,
  android.content,
  android.os,
  android.util,
  android.view,
  android.widget,
  android.graphics;
type
  MainActivity = public class(Activity)
  private
    colours : array[0 .. 4] of String := ['Red   ', 'Orange', 'Yellow', 'Green ', 'Blue  '];
    lv : ListView;
    tv : TextView;
    rg : RadioGroup;
    lstAdapter : ArrayAdapter<String>;     
  public
    method onCreate(savedInstanceState: Bundle); override;
    method ItemClicked(parent : AdapterView; view : View ; position : Integer ; id : Int64);
    method CheckChanged(rgp : RadioGroup; position : Integer);
  end;

implementation

method MainActivity.ItemClicked(parent : AdapterView ; view : View ; position : Integer ; id : Int64);
begin
  case position of 
    0 : tv.setTextColor(Color.parseColor("#FF0000"));  //red
    1 : tv.setTextColor(Color.parseColor("#FF8C00"));  //orange
    2 : tv.setTextColor(Color.parseColor("#FFFF00"));  //yellow
    3 : tv.setTextColor(Color.parseColor("#00FF00"));  //green
    4 : tv.setTextColor(Color.parseColor("#0000FF"));  //blue
  end;
end;
method MainActivity.CheckChanged(rgp : RadioGroup; position : Integer);
begin
  case position of 
    R.id.radioSmall : tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
    R.id.radioMedium : tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    R.id.radioLarge : tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); 
  end;
end;

method MainActivity.onCreate(savedInstanceState : Bundle);
begin
  inherited;
  // Set view from the "main" layout resource
  ContentView := R.layout.main;
  //Assign the ListView, TextView and RadioGroup.
  lv  := ListView(findViewById(R.id.lv1));
  tv  := TextView(findViewById(R.id.tv1));
  rg := RadioGroup(findViewById(R.id.rg1));
  //Assign the ArrayAdapter
  lstAdapter := new ArrayAdapter<String>(self, R.layout.simplerow, colours);  
  lv.setAdapter(lstAdapter); 

  lv.OnItemClickListener :=new interface AdapterView.OnItemClickListener(onItemClick := @ItemClicked);
  rg.OnCheckedChangeListener := new interface RadioGroup.OnCheckedChangeListener(onCheckedChanged := @CheckChanged);
end;

end.

Layout Files

<?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
      android:id="@+id/tv1"
      android:textSize="18sp"
      android:text="@string/instruction"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <ListView
      android:layout_centerHorizontal="true"
      android:id="@+id/lv1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />    
    <RadioGroup
      android:id="@+id/rg1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"  >
      <RadioButton
        android:id="@+id/radioSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_small" />
      <RadioButton
        android:id="@+id/radioMedium"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_medium"
        android:checked="true" />
      <RadioButton
        android:id="@+id/radioLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_large" />    
    </RadioGroup>
  </LinearLayout>


simplerow.layout-xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:padding="5dp"
  android:textSize="14sp" ></TextView>

Strings File

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">ListView and RadioGroup</string>
  <string name="instruction">Please click on a colour and select a text size.</string>
  <string name="radio_small">Small text</string>
  <string name="radio_medium">Medium text</string>
  <string name="radio_large">Large text</string>  
</resources>

Programming - a skill for life!

How to use EditText, Toast, DatePicker, ListView, RadioGroup, ScrollView and ImageView widgets, sound and files in Oxygene for Java Android apps