android - 如何在单击时启动列表视图项?
问题描述
我的列表视图显示用户安装的应用程序列表,但是如何获取单击项目的包名称?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView userInstalledApps = (ListView)findViewById(R.id.installed_app_list);
List<AppList> installedApps = getInstalledApps();
AppAdapter installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
}
private List<AppList> getInstalledApps() {
List<AppList> res = new ArrayList<AppList>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((isSystemPackage(p) == false)) {
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(new AppList(appName, icon));
}
}
return res;
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
}
这是我尝试过的,但是如何获取包名?
userInstalledApps.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
解决方案
请更改 applist.java 文件中的代码片段
private String name;
String package_name;
Drawable icon;
public AppList(String name, String package_name, Drawable icon) {
this.name = name;
this.icon = icon;
this.package_name = package_name;
}
public String getName() {
return name;
}
public Drawable getIcon() {
return icon;
}
public String getPackage_name() {
return package_name;
}
还要更改您的列表 java 文件代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView userInstalledApps = (ListView) findViewById(R.id.installed_app_list);
final List<AppList> installedApps = getInstalledApps();
AppAdapter installedAppAdapter = new AppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AppList appList = installedApps.get(i);
Log.e("pacakge name", appList.getPackage_name());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private List<AppList> getInstalledApps() {
List<AppList> res = new ArrayList<AppList>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((isSystemPackage(p) == false)) {
String appName = p.applicationInfo.loadLabel(getPackageManager()).toString();
Drawable icon = p.applicationInfo.loadIcon(getPackageManager());
String package_name = p.applicationInfo.packageName;
res.add(new AppList(appName, package_name, icon));
}
}
return res;
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true : false;
}
推荐阅读
- java - 正则表达式在每个字符串 html 中添加事件 onclick
- linux - 为什么使用 `unshare -m` 启动的进程会影响主机中的挂载?
- angular - Angular 在 url 上添加寻呼机以进行偏移和限制
- python - 为什么使用 PIL 和 Opencv 相同的像素具有不同的值?
- matplotlib - 在绘图中绘制其他数据时如何避免更改初始数据
- java - 处理 Servlet 中的异常和错误页面
- laravel - Laravel Nova - 将工具更改为资源工具
- uwp - 如何在 Windows 10 中将 UWP 应用程序的位置设置为与人员应用程序相同?
- c# - 与 Autofac 并行运行 xUnit 集成测试时无法加载类型 Castle.Proxies.IReadinessProxy
- reactjs - 如何在多个页面中使用 React Joyride