2011-05-16 135 views
0

幫助!所有的黑屏都一樣。因爲有必要添加代碼。謝謝。 我用這樣的代碼:如何獲取所有安裝的應用程序列表?

package com.tipfile; 

import java.util.ArrayList; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.content.pm.ResolveInfo; 


public class dop extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dop); 
    PackageManager pm = this.getPackageManager(); 

    Intent intent = new Intent(Intent.ACTION_MAIN, null); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 

    ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) 
    pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED); 
    for (ResolveInfo rInfo : list) { 
    System.out.println("Installed Applications " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()); 
    } 
    ListView listView = (ListView) findViewById(R.id.list); 
    ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); 
    listView.setAdapter(aa); 
}} 

回答

2
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); 
    listView.setAdapter(aa); 

你不初始化aa任何數據,因此黑屏。

您可以從ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>)建立List<String>變量和使用接受列表中的ArrayAdapter constructor填補aa

編輯

List<String> myList = new ArrayList<String>(); 
ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED); 
for (ResolveInfo rInfo : list) { 
    System.out.println("Installed Applications " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()); 
myList.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()); 
} 

ListView listView = (ListView) findViewById(R.id.list); 
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, myList); 
listView.setAdapter(aa); 
0
ArrayAdapter<String> aa = new 
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1); 

更改這些行到

ArrayAdapter<String> aa = new 
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list); 
相關問題