2017-09-05 42 views
0

我正在嘗試使用除系統應用程序之外的所有已安裝的應用程序填充微調控件。我遇到兩個問題:在Android微調控制器中重複的數據

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

回答

2

檢查這個代碼,我有建立和下面的代碼測試,也可以代替你的代碼這一條,

這會給你安裝包,在設備上的所有應用程序的源代碼目錄,將顯示所有包在旋轉

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

} 
+0

心不是這段代碼爲你工作?@大衛賈維斯 –

+0

嗨@Amit Vaghela泰爲您的文章和這方面的幫助!所以我剛剛用你的代碼替換了我的適配器代碼,我的微調現在顯示所有軟件包名稱而不是應用程序名稱,我的微調控制器也被填充了系統應用程序。我的微調項目必須顯示爲應用程序名稱,因爲用戶可能不會知道應用程序的軟件包名稱,我們也不能允許用戶從此列表中選擇系統應用程序。任何想法,我們可以如何修改代碼以適應此? –

+0

檢查我的更新答案。你的問題解決了@ DavidJarvis –