2011-11-23 1 views
0

Android 에뮬레이터에 설치된 응용 프로그램을 표시하기 위해이 코드를 시도했습니다. 그것은 성공적으로 컴파일되지만 작동하지 않습니다. 뭐가 문제 야? list.setAdapter에 전화에서getPackageManager를 사용하는 방법?

package pack.GetAllInstalledApplications; 
import java.util.ArrayList; 
import java.util.List; 
import android.app.Activity; 
import android.content.pm.PackageInfo; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class GetAllInstalledApplicationsExample extends Activity { 

public ArrayList <PackageInfoStruct> res = new ArrayList <PackageInfoStruct>(); 

public ListView list; 
public String app_labels[]; 


public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     getPackages(); 

     list = (ListView)findViewById(R.id.ListView01); 
     try{ 
      list.setAdapter(new ArrayAdapter<String>   (this,android.R.layout.simple_dropdown_item_1line,app_labels)); 
     }catch(Exception e){ 
      System.out.println("Err ++> " + e.getMessage()); 
       Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); 
     } 

} 
private ArrayList 
<PackageInfoStruct> getPackages() { 
    ArrayList 
<PackageInfoStruct> apps = getInstalledApps(false); 
    final int max = apps.size(); 
    for (int i=0; i < max; i++) { 
     apps.get(i); 
    } 
    return apps; 
} 

private ArrayList 
    <PackageInfoStruct> getInstalledApps(boolean getSysPackages) { 

    List 
    <PackageInfo> packs = getPackageManager().getInstalledPackages(0); 
    try{ 
     app_labels = new String[packs.size()]; 
    }catch(Exception e){ 
        Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_SHORT).show(); 
    } 
    for(int i=0;i < packs.size();i++) { 
     PackageInfo p = packs.get(i); 
     if ((!getSysPackages) && (p.versionName == null)) { 
      continue ; 
     } 
     PackageInfoStruct newInfo = new PackageInfoStruct(); 
     newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString(); 
     newInfo.pname = p.packageName; 
     newInfo.versionName = p.versionName; 
     newInfo.versionCode = p.versionCode; 
     newInfo.icon = p.applicationInfo.loadIcon(getPackageManager()); 
     res.add(newInfo); 

     app_labels[i] = newInfo.appname; 
    } 
    return res; 
} 
    } 
    class PackageInfoStruct {    
String appname = "";   
String pname = "";    
String versionName = "";  
int versionCode = 0;   
Drawable icon;     
    } 
+2

어떻게 작동하지 않는지에 대한 정보를 조금 더 주셔야합니다. 패키지를 표시하지 않습니까? 그것은 추락합니까? 그렇다면 오류 로그를 게시 할 수 있습니까? – Jack

+0

올바르게 컴파일되었지만 앱 아이콘을 찾지 못했습니다. – Angelika

답변

0

당신은 아마 다른 레이아웃을 사용해야합니다. 시도 했음 simple_list_item_1

list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, app_labels)); 

이 항목을 올바르게 표시합니다.

0

앱 라벨을 렌더링하기 전에 앱을 실제로 검색하고 있는지 이해하려면 몇 가지 로그를 추가해야합니다. 그 때 당신이 어댑터를 다룰 수 있다는 것을 확인했습니다.

ArrayAdapter의 생성자는 두 번째 매개 변수로 텍스트보기의 리소스 ID를 가져옵니다 (그렇지 않으면 충돌 함).

는 또한, 시스템 패키지를 폐기를 위해 나는이 코드 줄을 사용하십시오

... 경우 계속 ((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM를)! = 0) ...

여기서 pi는 PackageInfo입니다.

, 당신의 코드에 android.R.layout.simple_list_item_1를 사용하는 것이

+0

Unfortunatly 그것이 작동하지 않습니다. – Angelika

0

를 작동하는 경우 제가 어댑터 레이아웃을 변경 알게하고, 그것을 작동합니다.

-1

목록보기에 문제가있을 수 있습니다. ImageView 및 텍스트보기가 모두있는 사용자 지정 ListView Item을 만들어 패키지 드로어 블 이미지와 패키지 이름을 모두 표시 할 수 있습니다.

은 목록 항목을 만들 - list_single.xml

곳이 XML 파일을 리소스/레이아웃.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 
<ImageView 
android:id="@+id/icon" 
android:layout_width="50dp" 
android:layout_height="50dp" 
android:layout_marginBottom="5dp" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 
android:layout_marginTop="5dp" 
android:src="@drawable/ic_launcher" /> 
<TextView 
android:id="@+id/Itemname" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:textSize="20sp" 
android:paddingTop="5dp"/> 
</LinearLayout> 

main_activity.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" 
    tools:context="{relativePackage}.${activityClass}" > 

<ListView 
android:id="@+id/android:list" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" /> 
</RelativeLayout> 

마지막으로, 귀하의 MainActivity.java 파일

public class MainActivity extends ListActivity { 

String[] itemname ={ 
"Safari", 
"Camera", 
"Global", 
"FireFox", 
"UC Browser", 
"Android Folder", 
"VLC Player", 
"Cold War" 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

this.setListAdapter(new ArrayAdapter<String>(
this, R.layout.mylist, 
R.id.Itemname,itemname)); 
} 
} 

는 참고 :이 예제에서 당신이 대신 기본 레이아웃 유형 중 하나를 사용하는 것을 주 자체 정의 된 레이아웃을 사용하여 :

this.setListAdapter(new ArrayAdapter<String>(
this, R.layout.mylist, 
R.id.Itemname,itemname)); 
관련 문제