2

インストールされているアプリのリストを表示して、ユーザーがそのようなリストから複数のアプリを選択できるようにしたい。私はこれまでかなり成功し、アイコンを表示しましたが、リストの操作に行き詰まりました。タッチしてもアイテムが選択されず、ユーザーが完了したら選択したアイテムを取得する方法もわかりません。

コード:

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

1 に答える 1

1

これを私のアプリの1つに実装しました。これは、リストビュー用のカスタムアダプターを作成し、各行に選択したアイテムの名前を格納するための文字列配列のチェックボックスを配置することで実現できます。getView(にcheckchangeリスナーを実装します。 )アダプタの。チェックがtrueの場合は、名前配列内の位置を使用してリストアイテムを追加します。その逆も同様です。あなたが私のポイントを得たことを願っています...

于 2013-01-27T07:16:28.230 に答える