我正在尝试使用基本适配器在活动 1 中的列表中显示应用程序名称和图标,如下所示。
public class MainActivity extends Activity {
private static final String TAG_Name = "name";
private static final String TAG_Logo = "logo";
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView i = (TextView) findViewById(R.id.textView1);
ListAdapter mAdapter = ListAdapter.getInstance();
mAdapter.setAdapterData(this.getApplicationContext());
final ListView listView = (ListView) findViewById(R.id.my_list);
listView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();
i.setText(String.valueOf(ListAdapter.getInstance().getCount()));
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String a = ((TextView)
view.findViewById(R.id.text)).getText()
.toString();
int n = position;
Bitmap bitmap = ((BitmapDrawable)((ImageView)
view.findViewById(R.id.image))
.getDrawable()).getBitmap();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
Application_Perm.class);
in.putExtra(TAG_Name, a);
in.putExtra(TAG_Logo, bitmap);
in.putExtra("TAG", n);
startActivity(in);
}
});
}
}
但我不知道的是,当我想在活动 2 中显示包名称时,我传递了选择应用程序名称和图标。在这里我想显示所选应用程序名称的包名称。
public class Application_Perm extends Activity {
private static final String TAG_Name = "name";
private static final String TAG_Logo = "logo";
String name;
TextView pack;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.perm);
ImageView image = (ImageView) findViewById(R.id.logo);
TextView app = (TextView) findViewById(R.id.textpermapp);
pack = (TextView) findViewById(R.id.pa_info);
Intent intent = getIntent();
name = intent.getStringExtra(TAG_Name);
Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra(TAG_Logo);
app.setText(name);
image.setImageBitmap(bitmap);
}
}
基本适配器
public class ListAdapter extends BaseAdapter {
private static ListAdapter self = null;
private Context context = null;
private List<MyAppInfo> list = null;
private LayoutInflater mInflater = null;
private ListAdapter() {
}
public static ListAdapter getInstance() {
if (self == null) {
self = new ListAdapter();
}
return self;
}
public synchronized void setAdapterData(Context context) {
this.context = context;
mInflater = LayoutInflater.from(this.context);
list = new ArrayList<MyAppInfo>();
generate();
}
private void generate() {
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) {
list.add(new MyAppInfo("" + applicationInfo.loadLabel(pm),
applicationInfo.loadIcon(pm)));
}
}
}
public synchronized int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent, false);
}
MyAppInfo info = list.get(position);
TextView text = (TextView) convertView.findViewById(R.id.text);
ImageView image = (ImageView) convertView.findViewById(R.id.image);
text.setText(info.appName);
image.setImageDrawable(info.icon);
return convertView;
}
}