Adapter View



Gallery, ListView fP Spinner ^Sft VIEW AdapterView M^WMt^WMi:

# AdapterView Adapter Adapter t^S®, Tffi^-t^i:

1. Creates a Spinner with an existing View and binds it to a new ArrayAdapter that reads an array of colors from the local resources.

2. Creates another Spinner object from a View and binds it to a new SimpleCursorAdapter that will read people's names from the device contacts (seeContacts.People).

// Get a Spinner and bind it to an ArrayAdapter that

// references a String array.

Spinner s1 = (Spinner) findViewById(;

ArrayAdapter adapter = ArrayAdapter.createFromResource(

this, R.array.colors, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s1 .setAdapter(adapter);

// Load a Spinner and bind it to a data query. private static String[] PROJECTION = new String[] { People._ID, People.NAME

Spinner s2 = (Spinner) findViewById(;

Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);

SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, // Use a template // that displays a // text view cur, // Give the cursor to the list adatper new String[] {People.NAME}, // Map the NAME column in the

// people database to... new int[] {}); // The "text1" view defined in // the XML template adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item



// Create a message handling object as an anonymous class.

private OnltemClickListener mMessageClickedHandler = new OnItemClickListener() {

public void onItemClick(AdapterView parent, View v, int position, long id)

// Display a messagebox.

Toast.makeText(mContext,"You've got an event", Toast. LENGTH_SHORT). show();

// Now hook into our object and set its onltemClickListener member // to our class handler object.

mHistoryView = (ListView)findViewByld(; mHistoryView. setOnltemClickListener(mMessageClickedHandler);

0 0

Post a comment