android 微调器中的重复数据

Repeated data in 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

检查这段代码,我已经构建并测试了下面的代码,你也可以将你的代码替换成这个,

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

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);
  }

}