在android中有一组比较特别的组件,他们的使用有三部分来实现,M(数据模型),V(视图),C(控制)。以最典型的ListView为例,listView是用来显示数据的V(view),为ListView提供数据的数组或集合作为M(Model),而Adapter相当于C(Control)。由Adapter控制数据在控件上的显示。 今天,说说Adapter,当然,以ListView作为依托。
一、常用Adapter
1、Arrayadapter
2、SimpleAdapter
3、SimpleCursorAdapter
4、自定义Adapter
二、Adapter的使用方法
1、ArrayAdapter。
以程序说明在布局文件中,拖入一个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); } }
程序执行效果:
2、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); } }
程序执行效果:
3、SimpleCursorAdapter,这个先不讲了,为什么呀?因为我还不会呀,嘻嘻。以后会不上的。
4、自定义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) { } }); } }
程序执行效果: