0

ユーザーがインストールしたアプリをリスト ビューで一覧表示したい。@android:id や @+id などの多くのアプローチを試しましたが、何も機能していないようです..誰かがこのコードのエラーを指摘できます..

applist.xml

<?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" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout> 

listrow.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="10dp"  
    android:textSize="16sp"
    android:id="@+id/rowTextView">
</TextView>

InstalledApp クラス

public class InstalledApps extends ListActivity{

    private ListView listview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.applist);

        listview = (ListView)findViewById(R.id.listView1);

        PackageManager pm = getPackageManager();
        List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

        List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();

        for(ApplicationInfo app : packages) {
            //checks for flags; if flagged, check if updated system app
            if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
                //installedApps.add(app);
            //it's a system app, not interested
            } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
                //Discard this one
            //in this case, it should be a user-installed app
            } else {
                installedApps.add(app);
            }
        }

        ArrayAdapter<ApplicationInfo> adapter = new ArrayAdapter<ApplicationInfo>(this,
                R.layout.listrow, installedApps);
        listview.setAdapter(adapter);
    }
}
4

2 に答える 2

1

ListActivity には、画面の中央にある 1 つのフルスクリーン リストで構成される既定のレイアウトがあります。ただし、必要に応じて、onCreate() の setContentView() で独自のビュー レイアウトを設定することにより、画面レイアウトをカスタマイズできます。これを行うには、独自のビューに ID "@android:id/list" (コード内の場合はリスト)を持つ ListView オブジェクトが含まれている必要があります。

于 2012-07-05T02:53:57.920 に答える
1

あなたのコードはpublic class InstalledApps extends Activity問題ないようpublic class InstalledApps extends ListActivityですListActivityCaused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

于 2012-07-05T02:57:13.527 に答える