欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android ListActivity显示图标实例

程序员文章站 2022-07-11 19:40:55
首先,定义列表中的每一行,这里不是用xml文件定义,而是用一个类定义,checkbox、imageview、textview等控件以addview的方法添加。 复制代码...

首先,定义列表中的每一行,这里不是用xml文件定义,而是用一个类定义,checkbox、imageview、textview等控件以addview的方法添加。

复制代码 代码如下:

//apk列表的一行
class item_apk extends linearlayout{

    public checkbox chk_apk;
    public textview txt_name; 
    public textview txt_flag;
    public imageview img_apk;

    public item_apk(context ctx, string item_name, string item_flag, drawable item_draw)
    {

        super(ctx);
        this.setorientation(horizontal);

        chk_apk = new checkbox(ctx);
        addview(chk_apk,
                new linearlayout.layoutparams((int)(mainactivity.wid_scr*0.2),60));    

        img_apk = new imageview(ctx);
        img_apk.setimagedrawable(item_draw);
        addview(img_apk,
                new linearlayout.layoutparams((int)(mainactivity.wid_scr*0.2),60));

        txt_name = new textview(ctx);
        txt_name.settext(item_name);
        addview(txt_name,
                new linearlayout.layoutparams((int)(mainactivity.wid_scr*0.4),60));

        txt_flag = new textview(ctx);
        txt_flag.settext(item_flag);
        addview(txt_flag,
                new linearlayout.layoutparams((int)(mainactivity.wid_scr*0.2),60));

     }

}

然后,是定义列表,同样,也是用一个类来定义,这里的类继承自baseadapter。

复制代码 代码如下:

// apk列表
class list_apk extends baseadapter{

     private context ctx;
            private list<item_apk> list_data;

    public list_apk(context context){

        ctx = context;
        list_data = new arraylist<item_apk>();

    }

    @override
    public int getcount() {
        // todo auto-generated method stub
        return list_data.size();
    }

    @override
    public object getitem(int arg0) {
        // todo auto-generated method stub
        return list_data.get(arg0);
    }

    @override
    public long getitemid(int arg0) {
        // todo auto-generated method stub
        return list_data.indexof(arg0);
    }

    @override
    public view getview(int position, view convertview, viewgroup parent) {

        item_apk my_item;

        if (convertview==null)
        { 
            my_item = new item_apk(ctx,
                    (string)list_data.get(position).txt_name.gettext(),
                    (string)list_data.get(position).txt_flag.gettext(),
                    list_data.get(position).img_apk.getdrawable()); 
        }
        else
        { 
            my_item = (item_apk)convertview; 
            my_item.txt_name = list_data.get(position).txt_name; 
            my_item.txt_flag = list_data.get(position).txt_flag;
            my_item.img_apk = list_data.get(position).img_apk; 
        } 
        return my_item;

    }

    public void additem(string txt_name, string txt_flag, drawable ico_apk)
    { 
        list_data.add(new item_apk(ctx,txt_name,txt_flag,ico_apk));                
    }

}

最后,是activity的类,这里的activity类的oncreate(bundle savedinstancestate)里面没有setcontentview()方法,取而代之的是setlistadapter()方法。

复制代码 代码如下:

public class apk extends listactivity{

    @override
    protected void oncreate(bundle savedinstancestate) {

        super.oncreate(savedinstancestate);

        list_apk list_ada =  new list_apk(this);

        // 包管理器
        packagemanager pm = getpackagemanager();
        //获取手机内所有应用
        list<packageinfo> pi = pm.getinstalledpackages(0);

        list_ada.additem("应用名称",
                "是否系统应用",
                null); 

        for (int i=0; i<pi.size(); i++){

            packageinfo pii = (packageinfo) pi.get(i);
            string is_sys;
            drawable icon;

            if ((pii.applicationinfo.flags & pii.applicationinfo.flag_system) <= 0)
                is_sys = "否";
            else
                is_sys = "是";

            if (pii.applicationinfo.loadicon(pm)!=null)
                icon = (drawable)pii.applicationinfo.loadicon(pm);
            else
                icon = (drawable) getresources().getdrawable(r.drawable.ic_launcher);

             
            list_ada.additem(string.valueof(pii.applicationinfo.loadlabel(pm)),
                    is_sys,
                    icon);         

        }

        setlistadapter(list_ada);

    }

    @override
    public boolean oncreateoptionsmenu(menu menu) {
        // inflate the menu; this adds items to the action bar if it is present.
        getmenuinflater().inflate(r.menu.main, menu);
        return true;
    }

}

整个activity都是由类构成,没有用到一个xml布局文件。
运行效果如下。