2012-11-21 31 views
-1

我想列出由用户及其图标安装的应用程序。到目前为止,我设法获取用户安装的应用程序,但我无法将它与应用程序名称和图标放在一起。如何将应用程序前面的图标添加到列表中

列表:: {}图标test_application

活动

例如,行

ArrayList<String> listing = null; 
listing = new ArrayList<String>(); 
ArrayList<Object> icons = new ArrayList<Object>(); 
CharSequence c = null; 
int count = 0; 
Drawable icon = null; 

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TextView i = (TextView) findViewById(R.id.textView1); 

    PackageManager pm = getPackageManager(); 
    List<ApplicationInfo> packages = pm 
      .getInstalledApplications(PackageManager.GET_META_DATA); 


    for (ApplicationInfo applicationInfo : packages) { 
     if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) { 

      count = count + 1; 
      try { 
       c = pm.getApplicationLabel(pm.getApplicationInfo(
         applicationInfo.processName, 
         PackageManager.GET_META_DATA)); 
       icon = pm.getApplicationIcon(applicationInfo.packageName); 

      } catch (NameNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      listing.add(c.toString()); 
      icons.add(icon) 
 } 
    } 

    ListView list = (ListView) findViewById(R.id.listView1); 
    list.setAdapter(new ArrayAdapter<String>(this, R.layout.text, R.id.textView1, listing)); 
    String Counting = String.valueOf(count); 
    i.setText(Counting); 
} 

}

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     tools:context=".MainActivity" /> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" > 

    </ListView> 

</RelativeLayout> 

行XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/imageView1" 
     /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView1" 
     android:layout_toRightOf="@+id/textView1" 
     android:text="TextView" /> 

</RelativeLayout> 
+0

“但我无法把它在列表中的”准确你有什么问题? – Simon

+0

是什么问题?你没有得到列表视图? –

+0

你只有一个图标变量。你打算如何在列表中使用它?你的代码只会显示应用程序的名称,而不是图标... –

回答

0

This环节都有示例代码来创建文本和图像列表。

您需要将图标也存储在另一个数据结构中(可能是多个数组列表)。然后你将不得不创建自己的适配器来创建require视图。检查this张贴出例如代码..

下面是步骤需要遵循:

1)创建一类像以下:

class MyAppInfo{ 
public String appName; 
public Drawable icon; 
} 

2)现在改变数组列表拿这个对象像以下:

ArrayList<MyAppInfo> listing = null; 
listing = new ArrayList<MyAppInfo>(); 

3)现在在循环创建MyAppInfo的对象,并存储在该对象的应用程序和图标的名称,并添加OBJ像以下这样的数据列表。

PackageManager pm = getPackageManager(); 
    List<ApplicationInfo> packages = pm 
      .getInstalledApplications(PackageManager.GET_META_DATA); 
    MyAppInfo tempObj; 

    for (ApplicationInfo applicationInfo : packages) { 
     if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1) { 
       tempObj = new MyAppInfo(); 

      count = count + 1; 
      try { 
       c = pm.getApplicationLabel(pm.getApplicationInfo(
         applicationInfo.processName, 
         PackageManager.GET_META_DATA)); 
       icon = pm.getApplicationIcon(applicationInfo.packageName); 

       tempObj.appName = c.toString(); 
       tempObj.icon= icon; 

      } catch (NameNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      listing.add(tempObj); 

     } 
    } 

4)现在,创建自己的适配器,并使用这个列表中获取视图中创建ImageView的图标和TextView的应用程序名称...

+0

是Praful我仍然努力如何将此图标添加到列表中。如果你能为我推出代码,我会非常感谢 – Romi

+0

我建议你开始使用你的视图样本,并让我们更新你在使用代码时遇到的新问题。所以我们可以帮助... –

+0

是的,因为我应该知道我应该做什么1 ...在你给它谈论Hashmap ...但仍然没有给我一个解决方案。所以但即时通讯尝试与Hashmaps,它不是没有尝试。我只是想改善我的代码多数民众赞成 – Romi

相关问题