velis
velis

Reputation: 10025

AlertDialog MultiChoiceItems with icons

I want to display a list of installed apps where the user could select multiple apps in such a list. I was pretty successful so far, got the icons displayed, but got stuck at list manipulation: the items don't get selected when touched and I also don't know how to retrieve the selected ones once the user is done.

Code:

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

Upvotes: 2

Views: 606

Answers (1)

Vikram Singh
Vikram Singh

Reputation: 1420

I have implemented this in one of my apps.You can achieve this by creating a custom adapter for your list view and putting a checkbox in each row also an String array for storing the names of selected items.. Now implement checkchange listener in getView() of your adapter and if check is true add the list item with the help of position in name array and vice versa. Hope you got my point...

Upvotes: 1

Related Questions