2017-09-05 9 views
0

システムアプリ以外のすべてのインストール済みアプリケーションでスピナーを作成しようとしています。アンドロイドスピナーのデータを繰り返しました

1)私はは私のスピナーが移入されデータ

2)を繰り返し取得していますし、私はスピナー項目のリストを開き、時にはギャップが存在することができで間:私は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 Tyのあなたのポストのための&これで助けて!だから私はあなたのアダプタコードをあなたのものに置き換えました。私のスピナーはアプリケーション名ではなくすべてのパッケージ名を表示しています。スピナーにはシステムアプリが入っています。私のスピナー項目は、ユーザーがアプリのパッケージ名を知らないため、アプリ名として表示する必要があります。また、このリストからシステムアプリを選択することもできません。これに合うようにコードを修正する方法は? –

+0

更新された回答を確認してください。あなたの問題は解決されました@ DavidJarvis –

関連する問題