AdapterView
常用的有ListView,Spinner,GridView和Gallery
AdapterView只是容器,需要Adapter提供每一个列表项的组件
常用的Adapter包括:ArrayAdatper,SimpleAdapter,SimpleCursorAdapter和BaseAdapter
填充过程:
实例化Adapter →AdapterView.setAdatper(adapter);
(一)ArrayAdapter
ArrayAdapter的参数包含三项:Context,TextViewResourceId(ArrayAdapter只能是TextView),数组或者list
ArrayAdapter示例:
String [] str1=new String[]{"test1","test2","test3"}; ArrayAdapter<String>adapter1=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,str1); list2=(ListView)findViewById(R.id.list_view2); list1.setAdapter(adapter1);
(二)SimpleAdapter
SimpleAdapter包含的参数有:
1)Context
2)一个List<? Extends Map<String,?>>的集合对象
3)ViewResourceId
4)String[]类型,决定Map中哪些key对应的value来填充
5)int[]类型。填充哪些组件(往往用id的形式指出)
SimpleAdapter的示例:
ListView list1; private int touxiang[]=new int[]{R.drawable.a11,R.drawable.a22,R.drawable.a33,R.drawable.a44}; private String[] name=new String [] {"腾讯","Eclipse","魔兽世界","老滚5"}; private String[] descri=new String[] {"觉得自己很弱?充Q币啊魂淡","万能IDE","兽人永不为奴!","其实我是萝莉卷轴5"}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO 自动生成的方法存根 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list1=(ListView)findViewById(R.id.list_view1); List<Map<String,Object>>listItems=new ArrayList<Map<String, Object>>(); for (int i = 0; i < name.length; i++) { Map<String, Object>listItem=new HashMap<String, Object>(); listItem.put("touxiang",touxiang[i] ); listItem.put("name", name[i]); listItem.put("desci", descri[i]); listItems.add(listItem); } SimpleAdapter adapter=new SimpleAdapter(this, listItems, R.layout.simple_item, new String[]{"touxiang","name","desci"}, new int[]{R.id.touxiang,R.id.name,R.id.desci}); list1.setAdapter(adapter); list1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) { // TODO 自动生成的方法存根 System.out.println(name[arg2]+"被单击了"); } }); list1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO 自动生成的方法存根 System.out.println(name[arg2]+"被选中了"); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO 自动生成的方法存根 } }); }
(三)BaseAdapter
通过拓展BaseAdapter来实现Adapter对象
拓展时需要重写的方法有:
1)getCount() 返回列表的项目数
2)getItemId(int position)
3)getView(int position,View lineView,GroupView parent) 返回一个View 是每个列表项的内容
4)getItem(int position)
BaseAdapter示例:
ListView mListView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fifth); mListView=(ListView)findViewById(R.id.list_view3); BaseAdapter baseAdapter=new BaseAdapter() { @Override public View getView(int position, View lineView, ViewGroup parent) { // TODO 自动生成的方法存根 LinearLayout linearLayout=new LinearLayout(FifthActivity.this); ImageView imageView=new ImageView(FifthActivity.this); TextView textView =new TextView (FifthActivity.this); linearLayout.setOrientation(0); imageView.setImageResource(R.drawable.a11); textView.setText("第"+position+"企鹅"); linearLayout.addView(imageView); linearLayout.addView(textView); return linearLayout; } @Override public long getItemId(int arg0) { // TODO 自动生成的方法存根 return arg0; } @Override public Object getItem(int arg0) { // TODO 自动生成的方法存根 return null; } @Override public int getCount() { // TODO 自动生成的方法存根 return 10; } }; mListView.setAdapter(baseAdapter); }