Adapter分一下几种类别
ArrayAdapter:ListView那边基本上已经讲过了,没什么特殊的,就是一个数组
SimpleAdapter:比较强大了功能,基本上ListView都是用他的
SimpleCursorAdapter:和SimpleAdapter差不多,包装了cursor提供的数据
BaseAdapter:可以进行最大限度的定制,应为前面几种都是扩展BaseAdapter的
SimpleAdapter例子
item1.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView2" android:layout_alignBottom="@+id/textView2" android:layout_toRightOf="@+id/textView2" android:text="Medium Text" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#FF0000" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView1" android:text="Large Text"/> <ImageView android:id="@+id/imageView1" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="100dp" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/imageView1" android:layout_alignLeft="@+id/textView1" android:text="价格:" /> </RelativeLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- android:transcriptMode 设置成alwaysScroll,这样点击添加按钮后会自动滚动到最后 --> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity { Button btn; //内容 String[] names=new String[]{ "苹果(APPLE)iPhone 5c 16G版 3G手机(粉色)WCDMA/", "努比亚(nubia) 大牛 Z5S 3G手机(白色) WCDMA/TD-SC", "三星 Galaxy Note3 N9009 16G版 电信3G手机(简约白)C" }; //价格 String[] jgs=new String[]{"3299.00","1749.00","3399.00"}; //图片 int[] images=new int[]{R.drawable.pic1,R.drawable.pic2,R.drawable.pic3}; //id int[] ids=new int[]{ R.id.imageView1, R.id.textView1, R.id.textView3 }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个List的Map合集,将数据封装进去 List<Map<String,Object>> data=new ArrayList<Map<String,Object>>(); for(int i=0;i<names.length;i++){ Map<String,Object> map=new HashMap<String,Object>(); map.put("name", names[i]); map.put("jg", jgs[i]); map.put("image", images[i]); data.add(map); } //参数1:context //参数2:data List<Map<String,Object>>的集合 //参数2:item的布局 //参数4:maps的具体属性 //参数5:资源ID SimpleAdapter sa=new SimpleAdapter( MainActivity.this, data,R.layout.item1, new String[]{"image","name","jg"}, ids); ListView lv=(ListView)findViewById(R.id.listView1); lv.setAdapter(sa); } }
BaseAdapter
用这个实现上面的同样功能
item1.xml和activity_main.xml一样
MyAdapter.java
public class MyAdapter extends BaseAdapter{ private Context context; //内容 String[] names=new String[]{ "苹果(APPLE)iPhone 5c 16G版 3G手机(粉色)WCDMA/", "努比亚(nubia) 大牛 Z5S 3G手机(白色) WCDMA/TD-SC", "三星 Galaxy Note3 N9009 16G版 电信3G手机(简约白)C" }; //价格 String[] jgs=new String[]{"3299.00","1749.00","3399.00"}; //图片 int[] images=new int[]{R.drawable.pic1,R.drawable.pic2,R.drawable.pic3}; public MyAdapter(Context context){ this.context=context; } //一共有多少项 public int getCount() { // TODO 自动生成的方法存根 return names.length; } @Override public Object getItem(int position) { // TODO 自动生成的方法存根 return null; } @Override public long getItemId(int position) { // TODO 自动生成的方法存根 return position; } //返回每个ITEM显示的视图 public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView=LayoutInflater.from(context). inflate(R.layout.item1, null); } ImageView imageView1=(ImageView)convertView.findViewById(R.id.imageView1); TextView textView1=(TextView)convertView.findViewById(R.id.textView1); TextView textView3=(TextView)convertView.findViewById(R.id.textView3); imageView1.setImageResource(images[position]); textView1.setText(names[position]); textView3.setText(jgs[position]); return convertView; } }
MainActivity.java
public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MyAdapter sa=new MyAdapter(MainActivity.this); ListView lv=(ListView)findViewById(R.id.listView1); lv.setAdapter(sa); } }