1

我制作了一个应用程序,我可以在列表视图中查看我的所有应用程序,效果非常好。但是,我无法单击它,因为我还没有为其添加功能,如何将单击功能添加到列表视图?这是我的代码:

Java代码:

public class AllAppsActivity extends ListActivity {
LinearLayout appsLinearLayout;
ListView list;
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.allapps_screen);

    //Import views
    appsLinearLayout = (LinearLayout)findViewById(R.id.appsLinearLayout);

    //Set wallpaper
    appsLinearLayout.setBackgroundResource(R.drawable.images);

    //Load all apps
    final PackageManager pm = this.getPackageManager();

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

    final ArrayList<ResolveInfo> list =
            (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, 
                    PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list)
    {

        Log.i("TAG", ": Installed Applications " + rInfo.activityInfo.
                applicationInfo.loadLabel(pm).toString());
    }

    final ArrayAdapter<ResolveInfo> adapter = 
        new ArrayAdapter<ResolveInfo>(this, android.R.layout.simple_list_item_1, list)
        {
            @Override
            public View getView(int position, View convertView, ViewGroup parent)
            {
                convertView = super.getView(position, convertView, parent);
                final String text = list.get(position).activityInfo.
                    applicationInfo.loadLabel(pm).toString();
                ((TextView)convertView).setText(text);
                return convertView;
            }
        };
    setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
}
}
4

1 回答 1

1

将您的ArrayList<ResolveInfo> list(不要与您的另一个变量混淆ListView list)作为字段并在onListItemClick-method 中使用它:

private ArrayList<ResolveInfo> mApplicationList;

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    mApplicationList = (ArrayList<ResolveInfo>) pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
    ...
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final Intent intent = new Intent(Intent.ACTION_MAIN);
    final ActivityInfo info = mApplicationList.get(position).activityInfo;
    intent.setClassName(info.packageName, info.name);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    startActivity(intent);
}
于 2013-01-26T22:21:36.230 回答