在android中有一組比較特別的組件,他們的使用有三部分來實現,M(數據模型),V(視圖),C(控制)。以最典型的ListView爲例,listView是用來顯示數據的V(view),爲ListView提供數據的數組或集合做爲M(Model),而Adapter至關於C(Control)。由Adapter控制數據在控件上的顯示。 今天,說說Adapter,固然,以ListView做爲依託。java
1、經常使用Adapterandroid
一、Arrayadapter數組
二、SimpleAdapteride
三、SimpleCursorAdapter佈局
四、自定義Adapterthis
2、Adapter的使用方法spa
一、ArrayAdapter。3d
以程序說明在佈局文件中,拖入一個Listview,以後在程序中實現數據加載對象
public class MainActivity extends Activity { //定義Listview private ListView lv; //定義ArrayAdapter private ArrayAdapter<String> adapter; //定義要加載的數據 private String[] items={"oooo","uuuuu","mmmmm","vvvv"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //findViewById方法從佈局文件中獲得ListView實例化Lv lv=(ListView) findViewById(R.id.lv1); //實例化adapter,經過系統會提供的方式 adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,items); //爲Listview綁定Adapter lv.setAdapter(adapter); } }
程序執行效果:blog
二、SimpleAdapter。實例演示:
public class MainActivity extends Activity { private ListView lv; //定義一個adapter對象 private SimpleAdapter adapter; //定義SimpleAdapter加載的數據 private List<Map<String, Object>> list; private int[] imgid = { R.drawable.x1, R.drawable.x2, R.drawable.x3, R.drawable.x4, R.drawable.x5}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView) findViewById(R.id.listView1); list=new ArrayList<Map<String,Object>>(); Map<String,Object> map; //實例化list for(int i=0;i<imgid.length;i++){ map=new HashMap<String, Object>(); map.put("data", "ooo"+i); map.put("p_w_picpath", imgid[i]); list.add(map); } //實例化adapter adapter=new SimpleAdapter(this, list, R.layout.items, new String[]{"data","p_w_picpath"}, new int[]{R.id.textView1,R.id.p_w_picpathView1}); //綁定adapter lv.setAdapter(adapter); } }
程序執行效果:
三、SimpleCursorAdapter,這個先不講了,爲何呀?由於我還不會呀,嘻嘻。之後會不上的。
四、自定義Adapter。當前面的幾種Adapter沒法知足須要時,咱們會考慮使用自定義Adapter。它須要繼承BaseAdapter,而後至少要實現四個方法getCount(),getItem(),getItemId()及getView()方法。
實例演示:
自定義的Adapter:
public class MyAdapter extends BaseAdapter { private Context context; private String[] name; private String[] addr; private int[] imaids; public MyAdapter(Context context, String[] name, String[] addr, int[] imaids) { this.context = context; this.name = name; this.addr = addr; this.imaids = imaids; } @Override public int getCount() { return name.length; } @Override public Object getItem(int position) { return name[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.items, null); TextView tv1 = (TextView) convertView.findViewById(R.id.name); TextView tv2 = (TextView) convertView.findViewById(R.id.addr); ImageView iv = (ImageView) convertView .findViewById(R.id.p_w_picpathView1); iv.setBackgroundResource(imaids[position]); CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1); tv1.setText(name[position]); tv2.setText(addr[position]); cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Toast.makeText(context, "dajiaho", Toast.LENGTH_LONG) .show(); } }); } else { TextView tv1 = (TextView) convertView.findViewById(R.id.name); TextView tv2 = (TextView) convertView.findViewById(R.id.addr); CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox1); ImageView iv = (ImageView) convertView .findViewById(R.id.p_w_picpathView1); iv.setBackgroundResource(imaids[position]); tv1.setText(name[position]); tv2.setText(addr[position]); } return convertView; } }
Activity代碼:使用自定義的Adapter.
public class MainActivity extends Activity { private ListView lv; private MyAdapter adapter; private String[] name={ "金賢重","胡夏","林俊杰" }; private String[] addr={ "韓國.首爾","中國.青島","中國.大連" }; private int[] imaids={R.drawable.i2,R.drawable.hx,R.drawable.jj}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView) findViewById(R.id.listView1); adapter=new MyAdapter(this, name, addr,imaids); lv.setAdapter(adapter); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); } }
程序執行效果: