2017-12-08 22 views
-5

ここで、このAndroidデベロッパーページを参照: https://developer.android.com/guide/topics/ui/controls/spinner.html#PopulateスピナーにIDを追加するにはどうすればいいですか?

どのように私はIDを渡すと、リストが選択されたときに、このIDを選択することができますか? すなわち:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string-array name="planets_array"> 
    <item id=1>Mercury</item> 
    <item id=2>Venus</item> 
    <item id=3>Earth</item> 
    <item id=4>Mars</item> 
    <item id=5>Jupiter</item> 
    <item id=6>Saturn</item> 
    <item id=7>Uranus</item> 
    <item id=8>Neptune</item> 
</string-array> 
</resources> 

、ユーザーがアイテムをクリックしたときに、このIDを選択することができます。

+0

アレイの位置を使用する。または同じインデックスの別の配列にidを保存することができます –

+0

配列定義には 'id = n'のようなものはありません。あなたがリンクしたページを再度読んでください。 –

答えて

-2

idを配列に渡す必要はありません。自動的に0から開始されます。このように使用できます。ユーザがスピナーウィジェットから項目を選択すると、以下の方法をonItemSelected方法で今

public class SpinnerActivity extends Activity implements OnItemSelectedListener { 
... 

public void onItemSelected(AdapterView<?> parent, View view, 
     int pos, long id) { 
    // An item was selected. You can retrieve the selected item using 
    // parent.getItemAtPosition(pos) 
} 

public void onNothingSelected(AdapterView<?> parent) { 
    // Another interface callback 
} 

}

呼ばれる

<resources> 
<string-array name="planets_array"> 
    <item>Mercury</item> 
    <item>Venus</item> 
    <item>Earth</item> 
    <item>Mars</item> 
    <item>Jupiter</item> 
    <item>Saturn</item> 
    <item>Uranus</item> 
    <item>Neptune</item> 
</string-array> 
</resources> 
+0

このリストをあなたのスピナーに追加することができます。また、スピナーにセットした後、リストのIDと位置を取得することもできます。 –

-1

、一方は位置の値を見ることができます。この位置の値を使用して、選択した項目の位置を惑星文字列リソースファイルに取得できます。

String[] some_array = getResources().getStringArray(R.array.planets_array) 

値はOPが同様にこの質問を参照することができます。また

value = some_array[position value from the onItemSelected method ]; 

です。それが役に立てば幸い。

Can I give an ID to an item in String-array?

-1

あなたはあなたのためのIDとドロップダウン項目を開催しますスピナーアダプタを使用してカスタムオブジェクトを使用することができます。例えばのために:あなたはデータArrayListをスピンナーで、このアダプタクラスを使用することができます

public class PlanetData { 
    int id; 
    String planetName; 

    public void setId(int id) { 
     this.id = id; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setPlanetName(String planetName) { 
     this.planetName = planetName; 
    } 

    public String getPlanetName() { 
     return planetName; 
    } 

    // We will use this method to get the expand collapse string value of spinner. 
    @Override 
    public String toString() { 
     return planetName; 
    } 
} 

これは、カスタムクラスは次のようになります方法です。

import android.content.Context; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.support.annotation.NonNull; 
import android.support.v4.content.res.ResourcesCompat; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

import java.util.List; 

/** 
* <p> 
* Created by Angad Singh on 6/11/17. 
* </p> 
*/ 

public class TransactionSpinnerAdapter<T> extends ArrayAdapter<T> { 
    private Context context; 
    private List<T> objects; 

    public TransactionSpinnerAdapter(@NonNull Context context, int resource, @NonNull List<T> objects) { 
     super(context, resource, objects); 
     this.context = context; 
     this.objects = objects; 
    } 

    @NonNull 
    public TextView getView(int position, View convertView, @NonNull ViewGroup parent) { 
     TextView v = (TextView) super.getView(position, convertView, parent); 
     v.setText(objects.get(position).toString()); 
     return v; 
    } 

    public TextView getDropDownView(int position, View convertView, @NonNull ViewGroup parent) { 
     TextView v = (TextView) super.getView(position, convertView, parent); 
     v.setText(objects.get(position).toString()); 
     return v; 
    } 
} 

あなたActivity/Fragmentクラスでは、ちょうどあなたがRecyclerView/ListViewで使用するのと同じ方法でArrayListを使用しています。

// List of Planets 
ArrayList<PlanetData> planets = new ArrayList<>(); 
//... 
//... 

PlanetData planet1 = new PlanetData(); 
planet1.setId(1); 
planet1.setPlanetName("Mercury"); 
//... 
//... Create rest of your planets similarly. 
//... 

planets.add(planet1); 

とあなたのOnItemSelectedListenerのコールバックの内側だけで、リストの位置から、惑星のIDを取得します。

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
    int id = planets.get(pos).getId(); 
} 
関連する問題