package com.example.rookie10;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**创建用于listview指定列表项的适配器*/
String[] animals=new String[]{"人","pig","dog","duck"};
//创建适配器
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line,animals);
//设置在窗口中显示列表
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String result = l.getItemAtPosition(position).toString(); // 获取选择项的值
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
效果