Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android ListActivity顯示圖標實例

android ListActivity顯示圖標實例

編輯:關於Android編程

首先,定義列表中的每一行,這裡不是用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布局文件。
運行效果如下。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved