Elvenware Android Guide

Elvenware Logo

TOC

Toggle Menu

Charlie Calvert on Elvenware

Writing Code and Prose on Computers

Core Code

OS and Tools

Art

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:










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());

Docsiew.html"> Hello ListView

Copyright © Charlie Calvert | Elvenware Home | Writing Code | Delphi | CSharp | My Books