2012-11-21 28 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

相關問題