2016-08-08 9 views
-2

繰り返しのないアプリ名のリストを取得したいのですが、リストビューが既に同じ名前を持っていることをどのように知ることができますか?私は次のコードを試してみましたが、私は唯一のアプリケーションの複数のプロセスを実行しているので、私は唯一のもののためのアプリ名を表示したいこの状況で重複するアプリ名を取得しています。 どうすればいいですか?重複せずにお使いのデバイス内のすべてのパッケージ名を検索するためのListViewから重複したアプリ名(文字列)を削除します

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    final ViewHolder holder; 
    if (null == convertView) 
    { 
     LayoutInflater layoutInflater = (LayoutInflater) m_oContext 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = layoutInflater.inflate(R.layout.list_item, null); 
     holder = new ViewHolder(convertView); 
    } else 
    { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    AndroidAppProcess androidAppProcess = m_processesList.get(position); 
    //getItem(position); 
    TextView processName = (TextView) convertView.findViewById(R.id.name); 
    ImageView appIcon = (ImageView) convertView.findViewById(R.id.icon); 
    if (androidAppProcess != null) 
    { 
     try 
     { 

      String proName= (String) m_oContext.getPackageManager().getApplicationLabel(m_oContext.getPackageManager().getApplicationInfo(androidAppProcess.getPackageName(), m_oContext.getPackageManager().GET_META_DATA)); 
//   if(processName !=null && !m_processesList.contains(proName)) 
      if(processName !=null && !proName.equalsIgnoreCase((String)processName.getText())) 

      { 
       if(!proName.equals("Battery Optimizer")) 
       { 
        processName.setText(proName); 
        appIcon.setImageDrawable(m_oContext.getPackageManager().getApplicationIcon(androidAppProcess.getPackageName())); 
       } 
      } 
      //TODO: maintain icon size to avoid OOM error 
     } catch (PackageManager.NameNotFoundException e) 
     { 
      processName.setText(androidAppProcess.getPackageName()); 
      Drawable res = m_oContext.getResources().getDrawable(R.mipmap.ic_launcher, m_oContext.getTheme()); 
      appIcon.setImageDrawable(res); 
     } 

    } 
+0

スクリーンショットをしてください。 –

+3

単純適用HashSet はあなたの問題を解決します –

+0

@ S.W。それを解決することができますが、私はアダプタのgetView()の中でそれを使用する必要があります...あなたはそれを使用する方法を精巧にplzすることができます – sam

答えて

0

、単に以下の 1.

private List<PackageInfo> info = new ArrayList<PackageInfo>(); 

2. /内部コンストラクタでリストを開始する情報を格納するHashMapの変数を追加適用します@Override notifyDataSetChanged方法は

List<PackageInfo> packages = context.getPackageManager() 
      .getInstalledPackages(0); 
info = new ArrayList<PackageInfo>(); 
HashMap<String, PackageInfo> infoList = new HashMap<String, PackageInfo>(); 
for (PackageInfo s : packages) 
    if (!infoList.containsKey(s.packageName)){ 
     infoList.put(s.packageName, s); 
     info.add(s); 
    } 

3.

)(getViewメソッドでそれを適用します
processName.setText(info.get(position).packageName); 
+0

ます。private void detectApps(){ // TODOを適用します。ここアダプタ m_processesList = AndroidProcesses.getRunningAppProcesses()に実行しているアプリケーションのリストを設定します。 Set set = new HashSet <>(); set.addAll(m_processesList); m_processesList.clear(); m_processesList.addAll(set); runningAppsapter =新しいAppsAdapter(RunningAppsActivity.this、R.layout.list_item、m_processesList); m_listView.setAdapter(runningAppsAdapter); runningAppsAdapter。notifyDataSetChanged(); } – sam

関連する問題