First, define each line in the list. This is not defined by an xml file, but is defined by a class. Controls such as CheckBox, ImageView, TextView, etc. are added in the addView method.
Copy the codeThe code is as follows:
//A line of the apk list
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);
(HORIZONTAL);
chk_apk = new CheckBox(ctx);
addView(chk_apk,
new ((int)(MainActivity.wid_scr*0.2),60));
img_apk = new ImageView(ctx);
img_apk.setImageDrawable(item_draw);
addView(img_apk,
new ((int)(MainActivity.wid_scr*0.2),60));
txt_name = new TextView(ctx);
txt_name.setText(item_name);
addView(txt_name,
new ((int)(MainActivity.wid_scr*0.4),60));
txt_flag = new TextView(ctx);
txt_flag.setText(item_flag);
addView(txt_flag,
new ((int)(MainActivity.wid_scr*0.2),60));
}
}
Then, there is the definition list, which is also defined by a class, and the class here inherits from BaseAdapter.
Copy the codeThe code is as follows:
// apk list
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));
}
}
Finally, there is the Activity class. The onCreate(Bundle savedInstanceState) of the Activity class here does not have the setContentView() method, but instead is the setListAdapter() method.
Copy the codeThe code is as follows:
public class apk extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
list_apk list_ada = new list_apk(this);
// Package Manager
PackageManager pm = getPackageManager();
//Get all applications on the phone
List<PackageInfo> pi = (0);
list_ada.addItem("application name",
"Is it a system application?"
null);
for (int i=0; i<(); i++){
PackageInfo pii = (PackageInfo) (i);
String is_sys;
Drawable icon;
if (( & .FLAG_SYSTEM) <= 0)
is_sys = "No";
else
is_sys = "Yes";
if ((pm)!=null)
icon = (Drawable)(pm);
else
icon = (Drawable) getResources().getDrawable(.ic_launcher);
list_ada.addItem(((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(, menu);
return true;
}
}
The entire activity is composed of classes and does not use an XML layout file.
The operation effect is as follows.