2012-11-14 38 views
2

在我的應用程序中,我必須在列表視圖中列出已安裝的應用程序。應用程序在後臺線程中獲取已安裝的應用程序。要求是要填充列表事件,但是沒有完成提取操作,即如果在數組列表中有一個條目,則使用該條目顯示數組列表。這是可能的嗎?請幫助我。提前感謝使用當前存在的元素填充android列表

回答

2

首先,只有當您使用循環來處理背景中的應用程序數據時纔有可能。概要:

類舉行應用信息:

Class AppData { 
    public final Drawable icon; 
    public final String name; 

    public AppData(Drawable i, String n){ 
    this.icon = i; 
    this.name = n; 
    }  
} 

AsynckTask搜索應用程序:

AsyncTask<Void,AppData,Void> scanAppsTask = new AsyncTask<Void,AppData,Void>{ 

    @Override 
    public Void doInBackground(Void... args){ 

    //--get list--- 
    List<ApplicationInfo> apps = mPm.getInstalledApplications(
      PackageManager.GET_UNINSTALLED_PACKAGES | 
      PackageManager.GET_DISABLED_COMPONENTS); 

    //--run a loop-- 
    for(ApplicationInfo appInfo : apps){ 

     AppData newFound; 

     //---find app details, load app icon etc--- 

     publishProgress(newFound); 

    } 

    //---done--- 
    return null; 
    } 

    @Override 
    public void onProgressUpdate(AppData... data){ 
    //---update list for every app found---- 
    myListAdapter.add(data[0]); 
    myListAdapter.notifyDataSetChanged(); 
    } 
} 

scanAppsTask.execute(); 
+0

謝謝你。這個解決方案幫了我。 – user1767260

1

我正在研究一個類似的應用程序,我需要獲取所有已安裝的應用程序,甚至內置的應用程序。我用下面的代碼做了這個,

TextView data; 
    ImageView image1; 
    LinearLayout holdlayout; 
    View l1; 
    private ArrayList results = new ArrayList(); 
    List<ResolveInfo> list; 
    TextView result; 
    String str=""; 
    Drawable icon; 
     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      l1 = findViewById(R.id.Layout1); 
     PackageManager pm = this.getPackageManager(); 
     Intent intent = new Intent(Intent.ACTION_MAIN, null); 
     intent.addCategory(Intent.CATEGORY_LAUNCHER); 
     list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED); 
     for (ResolveInfo rInfo : list) 
     { 
      str = rInfo.activityInfo.applicationInfo.loadLabel(pm).toString() + "\n"; 
      results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()); 
      Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString()); 
      icon = rInfo.activityInfo.applicationInfo.loadIcon(pm); 
      holdlayout = new LinearLayout(getApplicationContext()); 
      holdlayout.setOrientation(LinearLayout.HORIZONTAL); 
      data = new TextView(getApplicationContext()); 
      data.setText(str); 
      image1 = new ImageView(getApplicationContext()); 
      image1.setBackgroundDrawable(icon); 
      ((ViewGroup) holdlayout).addView(image1); 
      ((ViewGroup) holdlayout).addView(data); 
      ((ViewGroup) l1).addView(holdlayout); 

     } 
     } 

讓我知道,如果您在此代碼中有任何問題。

+0

我有一個doubt.My後臺線程通過one.Then返回整個列表,而不是一個我如何填充列表 – user1767260

+0

您可以創建一個自定義列表適配器來填充lkist。 –

1

您可以從ListView的適配器中添加/刪除項目,所以是的,您可以在新項目可用時儘快從後臺線程填充項目列表。

相關問題