49

次の方法で Spinner を宣言します (非常に静的なのでarray.xml、タイトルと値用に 2 つの文字列配列があります) 。

<Spinner
    android:id="@+id/searchCriteria"
    android:entries="@array/searchBy"
    android:entryValues="@array/searchByValues" />

spinner.getSelectedItem()配列を返すことを期待し[title, value] ていますが、実際にはタイトル文字列だけを返します。無視 android:entryValuesですか?タイトルではなく値を取得するにはどうすればよいですか? これは XML でのみ実行できますか、それともアダプタを作成してプログラムで実行する必要がありますか?

4

3 に答える 3

136

デュアル配列メソッドではなく、プログラムで ArrayAdapter を既知の型のオブジェクトで埋めて、それを使用してみませんか。これを行う同様の性質のチュートリアル (下部のリンク) を作成しました。基本的な前提は、Java オブジェクトの配列を作成し、スピナーにそのことを伝え、それらのオブジェクトをスピナー クラスから直接使用することです。私の例では、次のように定義された「状態」を表すオブジェクトがあります。

package com.katr.spinnerdemo;

public class State {

// Okay, full acknowledgment that public members are not a good idea, however
// this is a Spinner demo not an exercise in java best practices.
public int id = 0;
public String name = "";
public String abbrev = "";

// A simple constructor for populating our member variables for this tutorial.
public State( int _id, String _name, String _abbrev )
{
    id = _id;
    name = _name;
    abbrev = _abbrev;
}

// The toString method is extremely important to making this class work with a Spinner
// (or ListView) object because this is the method called when it is trying to represent
// this object within the control.  If you do not have a toString() method, you WILL
// get an exception.
public String toString()
{
    return( name + " (" + abbrev + ")" );
}
}

次に、次のように、これらのクラスの配列をスピナーに設定できます。

       // Step 1: Locate our spinner control and save it to the class for convenience
    //         You could get it every time, I'm just being lazy...   :-)
    spinner = (Spinner)this.findViewById(R.id.Spinner01);

    // Step 2: Create and fill an ArrayAdapter with a bunch of "State" objects
    ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
          android.R.layout.simple_spinner_item, new State[] {   
                new State( 1, "Minnesota", "MN" ), 
                new State( 99, "Wisconsin", "WI" ), 
                new State( 53, "Utah", "UT" ), 
                new State( 153, "Texas", "TX" ) 
                });

    // Step 3: Tell the spinner about our adapter
    spinner.setAdapter(spinnerArrayAdapter);  

選択したアイテムは次のように取得できます。

State st = (State)spinner.getSelectedItem();

これで、操作する正真正銘の Java クラスができました。スピナーの値が変化したときに傍受したい場合は、OnItemSelectedListener を実装し、イベントを処理する適切なメソッドを追加するだけです。

public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{
    // Get the currently selected State object from the spinner
    State st = (State)spinner.getSelectedItem();

    // Now do something with it.
} 

public void onNothingSelected(AdapterView<?> parent ) 
{ 
}

ここでチュートリアル全体を見つけることができます: http://www.katr.com/article_android_spinner01.php

于 2010-10-30T16:22:37.327 に答える
55

ですから、スピナーにラベルと値の両方が必要なためにここに来た場合は、次のようにしました。

  1. 通常の方法でスピナーを作成するだけです
  2. array.xml ファイルで 2 つの等しいサイズの配列を定義します。1 つはラベル用、もう 1 つは値用
  3. スピナーを次のように設定しますandroid:entries="@array/labels"
  4. コードで-値が必要な場合は、次のようにします(チェーンする必要はありません)

    String selectedVal = getResources().getStringArray(R.array.values)[spinner
                             .getSelectedItemPosition()];
    

  5. 覚えておいてください - これら 2 つの配列は、番号スロットと位置に関して互いに一致する必要があります。
于 2009-10-19T17:15:49.110 に答える
8

中止、中止!何が私に入ったのかわかりませんが、属性Spinnerをサポートしていません。android:entryValuesそれは実際にListPreference似たようなことをするものです(ポップアップダイアログにアイテムのリストを表示します)。私が必要とするものについては、(悲しいかな)使用する必要がありますSpinnerAdapter

于 2009-10-19T05:48:35.500 に答える