在ListActivity中显示图标,好像并不复杂,实现起来却不轻松。
首先,定义列表中的每一行,这里不是用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布局文件。
运行效果如下。