3

Spinnerリストを取得したり、特定のタグに一致するすべてを検索したりすることはできますか?

ユーザーがその場で新しいウィジェットを追加できるようにしたいSpinnerのですが、各 から動的に値を取得できる必要がありますSpinner

ではjQuery、クラスに一致するすべての要素を で選択できます$('.myClassSelector').each()。これ、または同様のことを Android で行うことはできますか?

UPDATE すべてのスピナーはLinearLayout、XML で指定された特定のものにあります。レイアウトは、すべてのスピナーのコンテナーとして使用されます。

4

3 に答える 3

3

Spinner以前に追加したレイアウトのすべての子を取得して、子が追加されているかどうかを確認できると思いますSpinner

    LinearLayout ll = //Your Layout this can be any Linear or Relative layout 
                     //in which you added your spinners at runtime ;

    int count = ll.getChildCount();
    for(int i =0;i<count;i++)
    {
        View v = ll.getChildAt(i);
        if(v instanceof Spinner)
        {
            // you got the spinner
            Spinner s = (Spinner) v;
            Log.i("Item selected",s.getSelectedItem().toString());
        }
    }
于 2012-06-04T05:34:40.580 に答える
1

可能であれば、すべてのスピナーを同じ線形レイアウトで追加し、FasteKerinnsソリューションを使用することをお勧めしますが、不可能な場合は、以下のようなものを試してください。

Vector spinners = new Vector ():

private void treverseGroup(ViewGroup vg)
{
    final int count = vg.getChildCount();
    for (int i = 0; i < count; ++i)
    {
        if (vg.getChildAt(i) instanceof Spinner) 
        {

          spinners.add(vg.getChildAt(i));
        }
        else if (vg.getChildAt(i) instanceof ViewGroup)
            recurseGroup((ViewGroup) gp.getChildAt(i));
    }

}
于 2012-06-04T06:04:49.990 に答える
0

以下のメソッドには、 recursion を使用せずに、ルートにあるビュー階層全体のすべての Spinner を取得する機能があります。また、特定のタグに一致します。root

private ArrayList<Spinner> getSpinners(ViewGroup root, Object matchingTag) {
    ArrayList<?> list = root.getTouchables();

    Iterator<?> it = list.iterator();
    while (it.hasNext()) {
        View view = (View) it.next();
        if (!(view instanceof Spinner && view.getTag().equals(matchingTag))) {
            it.remove();
        }
    }

    return (ArrayList<Spinner>) list;
}
于 2012-06-05T14:58:32.307 に答える