android adapter 接口,AdapterView

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

// references a String array.

Spinner s1 = (Spinner) findViewById(R.id.spinner1);

ArrayAdapteradapter = 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(R.id.spinner2);

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 adapter

new String[] {People.NAME}, // Map the NAME column in the

// people database to...

new int[] {android.R.id.text1}); // The "text1" view defined in

// the XML template

adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

s2.setAdapter(adapter2);

Note that it is necessary to have the People._ID column in projection used with CursorAdapter

or else you will get an exception.

If, during the course of your application's life, you change the underlying data that is read by your Adapter,

you should call

Handling User Selections

You handle the user's selection by setting the class's

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

private OnItemClickListener 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 onItemClickListener member

// to our class handler object.

mHistoryView = (ListView)findViewById(R.id.history);

mHistoryView.setOnItemClickListener(mMessageClickedHandler);

For more discussion on how to create different AdapterViews, read the following tutorials:

Hello Spinner,

Hello ListView, and

Hello GridView.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值