Thursday, August 30, 2012

Simple ListView (android)


res/layout/list_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" >
</TextView>

res/values/list.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="data">
        <item>Apple</item>
        <item>Orange</item>
        <item>Banana</item>
        <item>Milk</item>
    </string-array>
</resources>


import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class HelloListViewActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // storing string resources into Array
        String[] data = getResources().getStringArray(R.array.data);
        // Binding resources Array to ListAdapter
        this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_cell, R.id.label, data));
    }
}




No comments:

Post a Comment