首页 > 解决方案 > 如何在单击时启动列表视图项?

问题描述

我的列表视图显示用户安装的应用程序列表,但是如何获取单击项目的包名称?

@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

        }
    });

标签: androidlistview

解决方案


请更改 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;
}

推荐阅读