我无法将我的活动扩展到listactivity。我想将其扩展到listactivity并将onclicklistener添加到列表项。

public class MainActivity extends Activity {

    private ListView lView;
    private ArrayList results = new ArrayList();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lView = (ListView) findViewById(R.id.lvApps);
        PackageManager pm = this.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        List < ResolveInfo > list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
        for (ResolveInfo rInfo: list) {
            results.add(rInfo.activityInfo.applicationInfo
                .loadLabel(pm).toString());
            Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
                .loadLabel(pm).toString());
        }
        lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
    }
}

最佳答案

如果要使用ListActivity,则不需要此行:

ListView lView = (ListView) findViewById(R.id.lvApps);


但是现在要引用的特定ListView(假设它在相应的xml布局中)必须将其id更改为

<ListView
  android:id="@android:id/list"
.....

关于java - 我无法将我的 Activity 扩展到listactivity。需要帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16563051/

10-10 09:46