简体   繁体   English

将点击功能添加到所有应用程序ListView Android

[英]Add click function to all apps listview android

I have made an application where i can see all of my apps in a listview which works very well. 我制作了一个应用程序,可以在列表视图中很好地查看所有应用程序。 However, i cannot click it because i haven't added a function for it, how can i add a click function to the listview? 但是,由于我尚未为其添加功能,因此无法单击它,如何将单击功能添加至列表视图? Here is my code: 这是我的代码:

Java code: 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) {
}
}

Make your ArrayList<ResolveInfo> list (not to be confused with your another variable ListView list ) as field and use it in onListItemClick -method: 将您的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM