-1

ユーザーがインストールしたアプリケーションとそのアイコンをリストアップしようとしています。これまでのところ、ユーザーがインストールしたアプリケーションを取得できましたが、アプリケーション名とアイコンを一緒にリストに入れるのに問題があります。

リストの例:: {icon} 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>
4

1 に答える 1

0

このリンクには、テキストと画像を含むリストを作成するサンプル コードがあります。

アイコンを別のデータ構造にも保存する必要があります(もう1つの配列リストである可能性があります)。次に、必要なビューを作成する独自のアダプターを作成する必要があります。コード例については、この投稿を確認してください。

従う必要がある手順は次のとおりです。

1) 次のような 1 つのクラスを作成します。

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

2) 配列リストを変更して、次のようにこのオブジェクトを取得します。

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

3) ループで MyAppInfo のオブジェクトを作成し、アプリの名前とアイコンをそのオブジェクトに保存し、そのオブジェクトを次のように 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);

        }
    }

ImageView4)独自のアダプターを作成し、このリストを取得ビューで使用して、アイコンとTextViewアプリ名を作成します...

于 2012-11-21T07:47:12.350 に答える