乐者为王

Do one thing, and do it well.

不使用ListActivity实现ListView

main.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ListView
        android:id="@+id/list_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

list_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0">
        <TextView
            android:id="@+id/app_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="16dip" />
        <TextView
            android:id="@+id/app_package"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>

创建一个继承自Activitiy的类,覆写onCreate方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

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

        ListView listView = (ListView)findViewById(R.id.list_view);
        SimpleAdapter adapter = new SimpleAdapter(this,
                getInstalledApps(false),
                R.layout.list_item,
                new String[] {"app_title", "app_package"},
                new int[] {R.id.app_title, R.id.app_package});
        listView.setAdapter(adapter);
    }

    private List<HashMap<String, Object>> getInstalledApps(boolean getSysPackages) {
        List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();

        List<PackageInfo> pkgs = getPackageManager().getInstalledPackages(0);
        for (int i = 0; i< pkgs.size(); i++) {
            PackageInfo pkg = pkgs.get(i);
            if (!getSysPackages && (pkg.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
                continue;
            }
            String label = pkg.applicationInfo.loadLabel(getPackageManager()).toString();
            String version = pkg.versionName;
            String pkgName = pkg.packageName;

            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("app_title", label + " " + version);
            map.put("app_package", pkgName);
            listItem.add(map);
        }
        return listItem;
    }
}

至此,一个不使用ListActivity的ListView就完成了。

Comments