上一篇咱們僅僅介紹了AdapterView的其中一個ListView,這一篇裏我將對其他的三個進行說明和講解。
java
1.Spinner(下拉列表)android
(1)MainActivity.javaapp
package com.example.l026_adapterview_other1; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Spinner; public class MainActivity extends Activity { private Spinner sp; private ArrayAdapter aAdapter; private String datas[]={"標準模式","靜音模式","振動模式","自定義模式"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp=(Spinner)findViewById(R.id.spinner1); aAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,datas); sp.setAdapter(aAdapter); } }
(2)activity_main.xmlide
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
(3)運行效果this
點擊下拉列表後:
xml
2.GridView(網格)blog
(1)MainActivity.javaget
package com.example.l027_adapterview_other2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widget.GridView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private GridView gv; private SimpleAdapter sAdapter; private int[] p_w_picpath={R.drawable.dice_1,R.drawable.dice_2, R.drawable.dice_3,R.drawable.dice_4,R.drawable.dice_5, R.drawable.dice_6,R.drawable.dice_action_0,R.drawable.dice_action_1, R.drawable.dice_action_2,R.drawable.dice_action_3,R.drawable.icon_002, R.drawable.icon_007,R.drawable.icon_010,R.drawable.icon_012, R.drawable.icon_012,R.drawable.icon_013,R.drawable.icon_018, R.drawable.icon_019,R.drawable.icon_019_cover,R.drawable.icon_020, R.drawable.icon_021,R.drawable.icon_021_cover,R.drawable.icon_022_cover, R.drawable.icon_022,R.drawable.icon_024,R.drawable.icon_024_cover, R.drawable.icon_027,R.drawable.icon_027_cover,R.drawable.icon_029, R.drawable.icon_029_cover,R.drawable.icon_030,R.drawable.icon_030_cover, R.drawable.icon_035,R.drawable.icon_035_cover,R.drawable.icon_040, R.drawable.icon_040_cover}; private String[] name={""}; private List<Map<String,Object>> lists; private Map<String,Object> maps; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gv=(GridView)findViewById(R.id.gridView1); lists=new ArrayList<Map<String,Object>>(); for(int i=0;i<p_w_picpath.length;i++){ maps=new HashMap<String,Object>(); maps.put("p_w_picpath", p_w_picpath[i]); lists.add(maps); } sAdapter=new SimpleAdapter(this, lists, R.layout.cell, new String[]{"p_w_picpath"}, new int[]{R.id.iv}); gv.setAdapter(sAdapter); } }
(2)MyAdapter.javait
package com.example.l027_adapterview_other2; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.Gallery.LayoutParams; import android.widget.ImageView; public class MyAdapter extends BaseAdapter{ private Context context; private int p_w_picpath[]={}; public MyAdapter(Context context,int[] p_w_picpath){ this.context=context; this.p_w_picpath=p_w_picpath; } @Override public int getCount() { return this.p_w_picpath.length; } @Override public Object getItem(int position) { return this.p_w_picpath[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView iv=new ImageView(context); iv.setBackgroundResource(p_w_picpath[position]); iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return iv; } }
3.Galleryio
(1)MainActivity.java
package com.example.l028_adapterview_gallery; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Gallery; import android.widget.Toast; public class MainActivity extends Activity { private Gallery gy; private MyAdapter mAdapter; private int[] p_w_picpath={ R.drawable.c0,R.drawable.c1,R.drawable.c2, R.drawable.c3,R.drawable.c4,R.drawable.c5, R.drawable.c6,R.drawable.c7,R.drawable.c8, R.drawable.c9,R.drawable.c10,R.drawable.c11, R.drawable.c12,R.drawable.c13,R.drawable.c14, R.drawable.c5}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gy=(Gallery)findViewById(R.id.gallery_main); mAdapter=new MyAdapter(this,p_w_picpath); gy.setAdapter(mAdapter); gy.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "已經設置爲壁紙", Toast.LENGTH_SHORT).show(); } }); } }
(2)MyAdapter.java
package com.example.l028_adapterview_gallery; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.Gallery.LayoutParams; import android.widget.ImageView; public class MyAdapter extends BaseAdapter{ private Context context; private int p_w_picpath[]={}; public MyAdapter(Context context,int[] p_w_picpath){ this.context=context; this.p_w_picpath=p_w_picpath; } @Override public int getCount() { return this.p_w_picpath.length; } @Override public Object getItem(int position) { return this.p_w_picpath[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView iv=new ImageView(context); iv.setBackgroundResource(p_w_picpath[position]); iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); return iv; } }