Androidアプリケーションでリストを表示する際、ListActivityは便利な手法です。以下にその使用方法を説明します。
まず、基本となるレイアウトファイルを定義します:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="項目1" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="アプリ名" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="システムアプリか否か" />
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/btn_uninstall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="アンインストール" />
</LinearLayout>
この中で最も重要なのはListViewです。
次に、リスト内の各アイテムを表現するためのレイアウトファイルを定義します:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/chk_select"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4" />
<TextView
android:id="@+id/txt_app_name"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="アプリ名" />
<TextView
android:id="@+id/txt_system_flag"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center_vertical"
android:text="フラグ" />
</LinearLayout>
最後に、SimpleAdapterを使用してリストを表示するクラスを実装します。
package com.example.listactivitydemo;
import android.app.ListActivity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AppListActivity extends ListActivity {
private List<Map<String, String>> appData = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// データ準備
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
for (ApplicationInfo app : apps) {
Map<String, String> item = new HashMap<>();
item.put("name", app.loadLabel(pm).toString());
item.put("flag", isSystemApp(app) ? "システムアプリ" : "ユーザーアプリ");
appData.add(item);
}
// アダプタ設定
SimpleAdapter adapter = new SimpleAdapter(
this,
appData,
R.layout.list_item,
new String[]{"name", "flag"},
new int[]{R.id.txt_app_name, R.id.txt_system_flag}
);
setListAdapter(adapter);
}
private boolean isSystemApp(ApplicationInfo info) {
return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
}
}
上記コードでは、getInstalledApplications()メソッドを使用してインストール済みアプリの一覧を取得し、それぞれのアプリがシステムアプリかどうかを判定しています。