android微调器中的重复数据

时间:2017-09-05 10:21:50

标签: android list adapter android-spinner

我尝试使用除系统应用之外的所有已安装应用程序填充微调器。我遇到了两个问题:

1)我正在重复数据

2)当我的微调器被填充并打开微调器项目列表时,应用程序名称之间有时会出现间隙

请参阅下面的适配器代码:

public class PackageAdapter extends BaseAdapter implements SpinnerAdapter {

private List<ApplicationInfo> applications;
private Context context;
private PackageManager packageManager = null;

public PackageAdapter(Context context) {
    this.context = context;

    // setting up the List applications with all the list of installed apps
    applications = new ArrayList<ApplicationInfo>();
    try {
        packageManager = context.getPackageManager();
        if (packageManager != null) {
            applications = packageManager.getInstalledApplications(0);
        }
    } catch (Exception e) {
        Log.e("error", e.getMessage());
    }

}

@Override
public int getCount() {
    return applications.size();
}

@Override
public Object getItem(int position) {
    return applications.get(position);
}

@Override
public long getItemId(int position) {
    return applications.indexOf(getItem(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View spinView;
    if (convertView == null) {
        // setting up the view
        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        spinView = inflater.inflate(R.layout.spin_layout, null);
    } else {
        spinView = convertView;
    }

    // getting one application from the list of apps.
    final ApplicationInfo application = this.applications.get(position);

    // filtering out the system apps and getting the application name
    String appName = "";
    if (packageManager.getLaunchIntentForPackage(application.packageName) != null) {
        appName = packageManager.getApplicationLabel(application).toString();
    }
    TextView t1 = (TextView) spinView.findViewById(R.id.field1);

    if (!appName.equals("")) {
        t1.setText(appName);
    }

    return spinView;
   }
}

对以上两种方面的任何帮助都将不胜感激! TY

1 个答案:

答案 0 :(得分:2)

检查此代码,我已经构建并测试了以下代码,您也可以将代码替换为此代码,

这将为您提供设备中所有应用程序的已安装包,Source dir ,将在微调器中显示所有包

public class PackageAdapter extends BaseAdapter implements SpinnerAdapter {

private List<String> applications = new ArrayList<>();
private Context context;
private String TAG = "PackageAdapter";
PackageManager pm;

public PackageAdapter(Context context) {
    this.context = context;

    pm = context.getPackageManager();
    // setting up the List applications with all the list of installed apps
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    for (ApplicationInfo packageInfo : packages) {

        if(!isSystemPackage(packageInfo)){
            Log.i(TAG, "Installed package :" + packageInfo.packageName);
            Log.i(TAG, "Source dir : " + packageInfo.sourceDir);
            applications.add(packageInfo.packageName);
        }
    }

}

@Override
public int getCount() {
    return applications.size();
}

@Override
public Object getItem(int position) {
    return applications.get(position);
}

@Override
public long getItemId(int position) {
    return applications.indexOf(getItem(position));
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View spinView;
    if (convertView == null) {
        // setting up the view
        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        spinView = inflater.inflate(R.layout.spin_layout, null);
    } else {
        spinView = convertView;
    }

    TextView t1 = (TextView) spinView.findViewById(R.id.field1);

    String packageNames = applications.get(position);
    if (packageNames != null && !packageNames.equalsIgnoreCase("")) {

        String appName = "";
        try {
            appName = (String) pm.getApplicationLabel(pm.getApplicationInfo(applications.get(position), PackageManager.GET_META_DATA));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        t1.setText(appName);
    }

    return spinView;
}


private boolean isSystemPackage(ApplicationInfo applicationInfo) {
    return ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
  }

}