2012-11-21 2 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 링크는 텍스트와 이미지 목록을 만들 수있는 샘플 코드가 있습니다.

아이콘을 다른 데이터 구조 (하나 이상의 배열 목록 일 수 있음)에도 저장해야합니다. 그런 다음 요구 사항보기를 작성하는 자체 어댑터를 작성해야합니다.

1) 다음과 같이 하나 개의 클래스를 만듭니다 .. this 확인 예제 코드를 위해 밖으로 게시

이 다음 단계는 당신이 따를 필요가 있습니다

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

2) 이제 취하도록 배열 목록을 변경 다음과 같은이 객체 :

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

3) 이제 루프 MyAppInfo의 개체를 만들고 그 개체의 응용 프로그램과 아이콘의 이름을 저장하고 OBJ를 추가 arraylist에게 다음과 같이 행동하십시오.

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을 만들 GET보기에이 목록을 사용 ...

+0

예 저는이 아이콘을 목록에 추가하는 방법에 여전히 어려움을 겪고 있습니다. u가 나를 위해 코드를 삽입 할 수 있다면 정말 좋을 것입니다. – Romi

+0

샘플을 사용하여 코드를 사용하면서 새로운 문제를 계속 업데이트 할 것을 제안합니다. 그래서 우리는 그걸 도울 수 있습니다 .... –

+0

예, 그렇습니다. 내가해야 할 일을 알아야합니다. ... Hashmap에 대해 이야기 한 내용에서 ...하지만 여전히 해결책을 찾지 못합니다. 그래서 해시 맵으로 시험해보고 있습니다. 난 그냥 내 코드가 전부를 개선하고 싶어요 모든 – Romi

관련 문제