从ListView中删除重复的应用程序名称(String)

时间:2016-08-08 07:05:40

标签: java android 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);
       }

    }

1 个答案:

答案 0 :(得分:0)

要检索设备内的所有包名而不重复,只需应用以下内容即可  1.添加HashMap变量来存储信息

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

2。在构造函数/内部@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);