Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Elvenware

Lists

On the one hand we have the List, which is a visual element, and on the other hand we have the adapter, which maps the data to the List. ListViews implement a widget called an AdapaterView The AdapterViewt and the data are separate, and the Adapter forms a bridge between them. We have several different kinds of Adapters, including:

Key elements:

You don't need a layout at all for a default list.

For a custom list give an id of list:

android:id="@android:id/list"

If you want a textview, use empty:

android:id="@android:id/empty"

Simple list program:

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

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, 
      new String[] { "1", "2", "3"});
 
  setListAdapter(adapter);
  }
}

It is also possible to have a custom ListView, rather than using the ListView from the system. If you do this, however, you should give your ListView a System ID. In this example called my_layout.xml, notice that the ListView has an ID of @android:id/list:

<?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/text1"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="Debut Text Appears here"
/>

<ListView
  android:id="@android:id/list"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

When you set things up this way, you can load the layout as usual, but you access the ListView in the layout as if it were a system resouce:

setContentView(R.layout.my_layout);
		
ArrayAdapter<String> array = new ArrayAdapter<String>(
  activity, 
  android.R.layout.simple_list_item_1,
  cursor.getColumnNames());

Links

Docsiew.html"> Hello ListView