android 的adapter介绍

来源:互联网 发布:成功网络促销案例 编辑:程序博客网 时间:2024/06/05 07:07

ListView是一种列表视图,其将ListAdapter所提供的各个控件显示在一个垂直且可滚动的列表中。需要注意的为创建适配器并将其设置给ListView。

1.ArrayAdapter

ArrayAdapter由3个参数进行构造,第一个为Context,第二个为在R文件中定义的Layout,也可用系统的R文件,第三个参数是一个数组,数组中每一项的类型没有限制。

系统默认的布局方式可通过android.R.layout.XX定义。

image

private static String[] data={"a","b","c","d"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.main);

            ListView listview=new ListView(this);
            ArrayAdapter adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
            listview.setAdapter(adapter);
            setContentView(listview);
    }

 

image

若自定义ListView中每一项TextView的样式arraylayout.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:gravity="center_horizontal" />

Activity中,指定ArrayAdapter第二个参数为arraylayout.xml:

private static String[] data={"a","b","c","d"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.main);

            ListView listview=new ListView(this);
            ArrayAdapter adapter=new ArrayAdapter<String>(this,R.layout.arraylayout,data);
            listview.setAdapter(adapter);
            setContentView(listview);
    }

image

2 SimpleAdapter

SimpleAdapter的ArrayList里的每一项都是一个Map<String,?>类型,每一项Map对象都和ListV中的一项进行数据绑定一一对应。

private ListView listview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listview=new ListView(this);
       data2 = new ArrayList<Map<String, Object>>();
        Map<String, Object> item;
        item = new HashMap<String, Object>();
        item.put("姓名", "张三");
        item.put("性别", "男");
        item.put("年龄", "25");
        data2.add(item);
        item = new HashMap<String, Object>();
        item.put("姓名", "李四");
        item.put("性别", "男");
        item.put("年龄", "33");
        data2.add(item);
        item = new HashMap<String, Object>();
        item.put("姓名", "小王");
        item.put("性别", "女");
        item.put("年龄", "31");
        data2.add(item);
        SimpleAdapter adapter = new SimpleAdapter(this, data2,
                R.layout.simplelayout, new String[] { "姓名", "性别","年龄" }, new int[] {
                        R.id.tv01, R.id.tv02,R.id.tv03 });
        listview.setAdapter(adapter);
        setContentView(listview);

其中ListView中每项的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal">
    <TextView android:id="@+id/tv01" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:width="150dp" />
    <TextView android:id="@+id/tv02" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:width="150dp"/>
    <TextView android:id="@+id/tv03" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:width="150dp"/>
</LinearLayout>

image

如果设置Activity的布局文件包含不仅ListView,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:src="@drawable/img01"/>
    <ListView android:id="@+id/listview01" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:choiceMode="singleChoice" />
</LinearLayout>

在Activity中:

setContentView(R.layout.main);
        listview=(ListView) findViewById(R.id.listview01);
        listview.setAdapter(adapter);

image

3 BaseAdapter

public class mainActivity extends Activity {
    /** Called when the activity is first created. */
    int [] drawableIds={R.drawable.img01,R.drawable.img02,R.drawable.img03};
    int [] msgIds={R.string.str1,R.string.str2,R.string.str3};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.main);
        ListView listview=(ListView) findViewById(R.id.listview01);
        BaseAdapter ba=new BaseAdapter() {
           
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                LinearLayout ll=new LinearLayout(mainActivity.this);
                ll.setOrientation(LinearLayout.HORIZONTAL);
                ll.setPadding(5, 5, 5, 5);
                ImageView ii=new ImageView(mainActivity.this);
                ii.setImageDrawable(getResources().getDrawable(drawableIds[position]));
                ii.setScaleType(ImageView.ScaleType.FIT_XY);
                ii.setLayoutParams(new Gallery.LayoutParams(50,50));
                ll.addView(ii);
                TextView tv=new TextView(mainActivity.this);
                tv.setText(getResources().getText(msgIds[position]));
                tv.setTextSize(24);
                tv.setTextColor(mainActivity.this.getResources().getColor(R.color.white));
                tv.setPadding(5, 5, 5, 5);
                tv.setGravity(Gravity.LEFT);
                ll.addView(tv);
                return ll;
            }
           
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }
           
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }
           
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 3;
            }
        };
        listview.setAdapter(ba);
    }
}

image

4 ListActivity

若使用ListActivity,则Activity里的ListView将充满屏幕。

在布局文件中,必须定义一个ListView,其Id为@id/android:list;另一个需要定义但并不是必须的是id为@id/android:empty的TextView,其为ListView中无数据时显示的内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ListView android:id="@id/android:list" android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <TextView android:id="@id/android:empty" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:text="没有任何数据" />
</LinearLayout>

Activity中,ListView每行设置和之前方法一样。

String [] data={"a","b","c","d"};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   
    setContentView(R.layout.main);
    setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data));
   
}

 

image

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 诲信电冰箱电脑板坏了怎么办 冰箱里放了热水后就不制冷了怎么办 双温冰柜冷藏矿泉水不冰怎么办 美的电饭煲e一传感器也没坏怎么办 美的电饭煲不工作显示C3怎么办 美的电饭煲啪一声响不工作了怎么办 误给宝宝吃了坏的饭怎么办 鼠标没反应键盘指示灯不亮怎么办 新买变频冰箱风机声音大怎么办 三星手机玩王者荣耀一直闪退怎么办 刚申请的阿里大宝卡不想要了怎么办 国美刚买不到一个月电视坏了怎么办 交保险后保险公司不给开收据怎么办 收据白联作废红联丢失怎么办 收据作废客户联给客户了怎么办 宜家买的床和床垫搬家了怎么办 科龙空调开机后自己关机怎么办 以旧换新旧的没给商家 报案怎么办 想换新手机但是旧的没坏怎么办 从苏宁易购买的电视坏了怎么办 苏宁易购服务站买的电视坏了怎么办 用微信登陆京东账号退不出来怎么办 微信账号密码手机号都忘记了怎么办 我的手机号京东被别人绑定了怎么办 京东之前绑定的手机号不用了怎么办 京东退货钱未到银行卡账号里怎么办 京东身份证绑的手机号不用了怎么办 海尔冰箱要退货箱子扔了怎么办 海尔冰箱门变形关不严没吸力怎么办 长时间不用的手机忘记密码怎么办啊 微信忘记密码手机号又不用了怎么办 华为账号换了手机密码忘记了怎么办 用u盘制作音响喊话内容怎么办 新三板公司退市了小股东怎么办? 新三板公司退市股东股票怎么办 利群收购乐天玛特超市卡怎么办 买房过户夫妻一方是外省户口怎么办 欠了国美金融贷款没钱还会怎么办? 国企员工涨工资不在编的员工怎么办 装车牌照的螺丝孔小了怎么办 北京摇号之后中签和未中签怎么办