添加点击功能到所有应用listview android

时间:2013-01-26 21:51:39

标签: java android listview android-listview

我已经创建了一个应用程序,我可以在列表视图中查看我的所有应用程序。但是,我无法点击它,因为我没有为它添加功能,如何在列表视图中添加点击功能?这是我的代码:

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) {
}
}

1 个答案:

答案 0 :(得分:1)

将您的ArrayList<ResolveInfo> list(不要与其他变量ListView list混淆)作为字段,并在onListItemClick中使用 - 方法:

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);
}