android ListActivity displays icon instances
- 2020-05-19 05:44:14
- OfStack
First, define each line in the list, not with the xml file, but with a class definition, CheckBox, ImageView, TextView and other controls added in the addView method.
//apk A list of 1 line
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));
}
}
Then, the definition list, again, is defined in 1 class, which is inherited from BaseAdapter.
// apk The list of
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, the Activity class, where the Activity class onCreate(Bundle savedInstanceState) does not have the setContentView() method, but instead has the setListAdapter() method.
public class apk extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list_apk list_ada = new list_apk(this);
// A package manager
PackageManager pm = getPackageManager();
// Get all the apps on your phone
List<PackageInfo> pi = pm.getInstalledPackages(0);
list_ada.addItem(" The application name ",
" System application ",
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 = " no ";
else
is_sys = " is ";
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;
}
}
The entire Activity is made up of classes and does not use an xml layout file.
The effect is as follows.