gpt4 book ai didi

java - 为所有应用程序 ListView android添加点击功能

转载 作者:行者123 更新时间:2023-11-30 03:48:34 24 4
gpt4 key购买 nike

我做了一个应用程序,我可以在 ListView 中看到我所有的应用程序,它运行良好。但是,我不能点击它,因为我没有为它添加一个功能,我怎样才能为 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) {
}
}

最佳答案

让你的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);
}

关于java - 为所有应用程序 ListView android添加点击功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14541938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com