我将ListView
使用安装在 Android 设备上的应用程序的图标和标签进行自定义我编写了一些代码,但我不知道为什么当我运行应用程序时我ListView
是空的,这里是代码:
public class MainActivity extends Activity {
ArrayList<String> AppLabel = new ArrayList<String>();
ArrayList<Drawable> AppIcon = new ArrayList<Drawable>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView MyListView = (android.widget.ListView) findViewById(R.id.listView1);
EditText MyEditText = (android.widget.EditText) findViewById(R.id.textView1);
CustomAdapter Adapter = new CustomAdapter(this, android.R.layout.simple_list_item_multiple_choice,
MyEditText, AppLabel);
MyListView.setAdapter(Adapter);
PackageManager pm = this.getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
for(ApplicationInfo app : apps) {
String Label = (String)pm.getApplicationLabel(app);
AppLabel.add(Label);
Drawable Icon = pm.getApplicationIcon(app);
AppIcon.add(Icon);
}//End for
Adapter.notifyDataSetChanged();
}//end onCreate
class CustomAdapter extends ArrayAdapter<string>{
public CustomAdapter(Context context, int resource,
EditText myEditText, ArrayList<String> appLabel) {
super(context, resource);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row==null) {
LayoutInflater Inflater = getLayoutInflater();
row = Inflater.inflate(R.layout.item_layout, parent, false);
}//End if
EditText MyEditText = (android.widget.EditText) findViewById(R.id.textView1);
MyEditText.setText(AppLabel.get(position));
ImageView MyImageView = (ImageView) findViewById(R.id.imageView1);
MyImageView.setImageDrawable(AppIcon.get(position));
return (row);
}//End get view
}//End CustomAdapter Class
}//end Main Class