Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有图标的AlertDialog多选项项_Android_Android Alertdialog - Fatal编程技术网

Android 带有图标的AlertDialog多选项项

Android 带有图标的AlertDialog多选项项,android,android-alertdialog,Android,Android Alertdialog,我想显示已安装应用的列表,用户可以在该列表中选择多个应用。 到目前为止,我非常成功,显示了图标,但在列表操作方面遇到了困难:当触摸这些项目时,它们不会被选中,而且我也不知道在用户完成操作后如何检索选定的项目 代码: PackageManager pm=getPackageManager()//获取已安装应用程序的列表。 List packages=pm.getinstalledApplication(PackageManager.GET_元数据); 最终ArrayList应用程序=新的Array

我想显示已安装应用的列表,用户可以在该列表中选择多个应用。 到目前为止,我非常成功,显示了图标,但在列表操作方面遇到了困难:当触摸这些项目时,它们不会被选中,而且我也不知道在用户完成操作后如何检索选定的项目

代码:

PackageManager pm=getPackageManager()//获取已安装应用程序的列表。
List packages=pm.getinstalledApplication(PackageManager.GET_元数据);
最终ArrayList应用程序=新的ArrayList(packages.size());
对于(ApplicationInfo packageInfo:packages)
{
log.i(“获取软件包列表”,“安装的软件包:%s名称%s”,packageInfo.packageName,pm.getApplicationLabel(packageInfo));
apps.add(新的AppsItem(packageInfo.packageName,pm.getapplication(packageInfo),pm.getApplicationLabel(packageInfo.toString());
}
集合。排序(应用程序);
final ListAdapter=new ArrayAdapter(这是android.R.layout.select\u dialog\u multichoice,android.R.id.text1,apps)
{
公共视图getView(int位置、视图转换视图、视图组父视图)
{
//用于创建视图的用户超类
视图v=super.getView(位置、转换视图、父级);
CheckedTextView tv=(CheckedTextView)v.findViewById(android.R.id.text1);
最终AppsItem itm=apps.get(位置);
tv.setText(itm.appText);
//将图像放在文本视图上
tv.setCompoundDrawablesWithIntrinsicBounds(itm.icon,null,null);
tv.setChecked(itm.selected);
setOnClickListener(新的OnClickListener()
{
公共void onClick(视图)
{
CheckedTextView v=(CheckedTextView)视图;
itm.selected=!itm.selected;
v、 setChecked(已选择itm);
}
});
//在图像和文本之间添加边距(支持各种屏幕密度)
int dp5=(int)(5*getResources().getDisplayMetrics().density+0.5f);
tv.setCompoundDrawablePadding(dp5);
返回v;
}
};
AlertDialog.Builder alert=新建AlertDialog.Builder(Settings.this);
警报。设置标题(rTitle);
alert.setAdapter(适配器,null);
alert.setPositiveButton(TX.s(android.R.string.ok),新的DialogInterface.OnClickListener()
{
@凌驾
public void onClick(DialogInterface dialog,int which)
{
字符串selApps=“”;
用于(AppsItem应用程序:应用程序)
如果(已选择应用程序)
selApps+=app.appID+“,”;
如果(selApps.length()>0)
selApps=selApps.substring(0,selApps.length()-1);
log.i(“应用程序选择”,“所选应用程序:%s”,selApps);
}}); //如何在此处检索单击的项目?
alert.setNegativeButton(TX.s(android.R.string.cancel)),null;
alert.show();

我已经在我的一个应用程序中实现了这一点。您可以通过为列表视图创建自定义适配器,并在每行中放置复选框以及用于存储所选项目名称的字符串数组来实现这一点。。现在在适配器的getView()中实现checkchange listener,如果check为true,则在名称数组中的位置帮助下添加列表项,反之亦然。希望你明白我的意思……

是的,我明白了。我觉得奇怪的是,这将是实现这一目标的唯一途径。我希望有更优雅的东西:)编辑:用解决方案更新样本
PackageManager pm = getPackageManager(); //get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
final ArrayList<AppsItem> apps = new ArrayList<AppsItem>(packages.size());
for (ApplicationInfo packageInfo : packages)
{
  log.i("getting package list", "Installed package : %s  name %s", packageInfo.packageName, pm.getApplicationLabel(packageInfo));
  apps.add(new AppsItem(packageInfo.packageName, pm.getApplicationIcon(packageInfo), pm.getApplicationLabel(packageInfo).toString()));
}
Collections.sort(apps);

final ListAdapter adapter = new ArrayAdapter<AppsItem>(this, android.R.layout.select_dialog_multichoice, android.R.id.text1, apps)
{
  public View getView(int position, View convertView, ViewGroup parent)
  {
    //User super class to create the View
    View v = super.getView(position, convertView, parent);
    CheckedTextView tv = (CheckedTextView) v.findViewById(android.R.id.text1);
    final AppsItem itm = apps.get(position);

    tv.setText(itm.appText);
    //Put the image on the TextView
    tv.setCompoundDrawablesWithIntrinsicBounds(itm.icon, null, null, null);
    tv.setChecked(itm.selected);

    tv.setOnClickListener(new OnClickListener()
    {
      public void onClick(View view)
      {
        CheckedTextView v = (CheckedTextView) view;
        itm.selected = !itm.selected;
        v.setChecked(itm.selected);
      }
    });

    //Add margin between image and text (support various screen densities)
    int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
    tv.setCompoundDrawablePadding(dp5);

    return v;
  }
};

AlertDialog.Builder alert = new AlertDialog.Builder(Settings.this);

alert.setTitle(rTitle);
alert.setAdapter(adapter, null);
alert.setPositiveButton(TX.s(android.R.string.ok), new DialogInterface.OnClickListener()
{
  @Override
  public void onClick(DialogInterface dialog, int which)
  {
    String selApps = "";
    for (AppsItem app: apps)
      if (app.selected)
        selApps += app.appID + ",";
    if (selApps.length() > 0)
      selApps = selApps.substring(0, selApps.length() - 1);
    log.i("app selection", "selected apps: %s", selApps);            
  }}); //How to retrieve the clicked items here?
alert.setNegativeButton(TX.s(android.R.string.cancel), null);
alert.show();