2016-10-04 2 views
0

私はSpinnerを持っています、それはJSONを解析していくつかのアイテムを持っています。 JSONは私のアプリ "statusId"と "statusTitle"で送信します。 JSON "statusID"を "statusTitle"に設定する方法は、次のステップでgetSelectItemを "statusId"と呼ぶことができますか?スピナーのアイテムにどのようにIDを割り当てましたか?

getSelectItemId "statusId"と "positioId"が異なるため、この状況では不適切です。

P.S.私の質問を厳粛に受け止めないでください、ありがとうございます。

答えて

0

このように、両方のArraylistに両方を保存します。

ArrayList<String> title = new ArrayList<>(); 
    title.add("A"); 
    title.add("B"); 
    title.add("C"); 
    title.add("D"); 

    ArrayList<Integer> id = new ArrayList<>(); 
    id.add(1); 
    id.add(2); 
    id.add(3); 
    id.add(4); 

Now title Aはid 1、Bはid 2などです。今すぐあなたが項目のいずれかを選択するたびに。その位置を取得し、その位置の両方のarraylistの項目を確認します。

public class Item { 
    public int id; 
    public String label; 

    @Override 
    public String toString() { return label; } 
} 

は、ArrayListのか、あなたのArrayAdapterの項目の配列を追加し、そうスピナーにアダプタを追加します。あなたは、スピナーであなたの項目をリストするには、両方のidとタイトル

title.get(position of selected item); 
id.get(position of selected item); 
0

使用クラスを取得します。

はスピナーから項目を選択するには:

Item item = (Item) spinner.getSelectedItem(); 

およびIDへのアクセス私の英語のためのitem.id

申し訳ありませんと。

0

このようなItemNameのとアイテムIDを持つクラスを作成します。

public class EncapTest { 
    private String name; 
    private String idNum; 



    public String getName() { 
     return name; 
    } 

    public String getIdNum() { 
     return idNum; 
    } 


    public void setName(String newName) { 
     name = newName; 
    } 

    public void setIdNum(String newId) { 
     idNum = newId; 
    } 
} 

やカスタムアダプタを使用して、これを移入します。

1

クラスを作成してキー値を保存し、アダプタを作成します。

Spinner keyValueSpinner = (Spinner) rootView.findViewById(R.id.spinner_key_value); 

KeyValueArrayAdapter adapter = new KeyValueArrayAdapter(getActivity(),android.R.layout.simple_spinner_item); 
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
adapter.setEntries(getResources().getStringArray(R.array.statusTitle)); 
adapter.setEntryValues(getResources().getStringArray(R.array.statusId)); 

keyValueSpinner.setAdapter(adapter) 

(注)この例では、リソースでarray.xml内側の配列とアダプタを移入することを:あなたはそのような何かを行うことができますスピナーを移入する

。しかし、あなたはString []を入力することができます。

とクラスアダプタKeyValueArrayAdaper.java

public class KeyValueArrayAdapter extends ArrayAdapter<KeyValueArrayAdapter.KeyValue> { 

    /** 
    * Key and Value 
    */ 
    public class KeyValue { 
     public String key; 
     public String value; 

     /** 
     * @param key 
     * @param value 
     */ 
     public KeyValue(final String key, final String value) { 
      super(); 
      this.key = key; 
      this.value = value; 
     } 

    } 

    /** 
    * @param context 
    * @param resource 
    * @param textViewResourceId 
    * @param objects 
    */ 
    public KeyValueArrayAdapter(final Context context, final int resource, 
           final int textViewResourceId, 
           final KeyValue[] objects) { 
     super(context, resource, textViewResourceId, objects); 
    } 

    /** 
    * @param context 
    * @param resource 
    * @param textViewResourceId 
    * @param objects 
    */ 
    public KeyValueArrayAdapter(final Context context, final int resource, 
           final int textViewResourceId, 
           final List<KeyValue> objects) { 
     super(context, resource, textViewResourceId, objects); 
    } 

    /** 
    * @param context 
    * @param resource 
    * @param textViewResourceId 
    */ 
    public KeyValueArrayAdapter(final Context context, final int resource, 
           final int textViewResourceId) { 
     super(context, resource, textViewResourceId); 
    } 

    /** 
    * @param context 
    * @param textViewResourceId 
    * @param objects 
    */ 
    public KeyValueArrayAdapter(final Context context, final int textViewResourceId, 
           final KeyValue[] objects) { 
     super(context, textViewResourceId, objects); 
    } 

    /** 
    * @param context 
    * @param textViewResourceId 
    * @param objects 
    */ 
    public KeyValueArrayAdapter(final Context context, final int textViewResourceId, 
           final List<KeyValue> objects) { 
     super(context, textViewResourceId, objects); 
    } 

    /** 
    * @param context 
    * @param textViewResourceId 
    */ 
    public KeyValueArrayAdapter(final Context context, final int textViewResourceId) { 
     super(context, textViewResourceId); 
    } 

    /** 
    * Change the string value of the TextView with the value of the KeyValue. 
    */ 
    @Override 
    public View getView(final int position, final View convertView, final ViewGroup parent) { 
     final TextView view = (TextView) super.getView(position, convertView, parent); 

     view.setText(getItem(position).value); 
     return view; 
    } 

    /** 
    * Change the string value of the TextView with the value of the KeyValue. 
    */ 
    @Override 
    public View getDropDownView(final int position, final View convertView, final ViewGroup parent) { 
     final TextView view = (TextView) super.getDropDownView(position, convertView, parent); 

     view.setText(getItem(position).value); 
     return view; 
    } 

    /** 
    * Set the specified Collection at the array. 
    * 
    * @param keys 
    * @param vaules 
    */ 
    public void setKeyValue(final String[] keys, final String[] vaules) { 
     if (keys.length != vaules.length) { 
      throw new RuntimeException("The length of keys and values is not in agreement."); 
     } 

     final int N = keys.length; 
     for (int i = 0; i < N; i++) { 
      add(new KeyValue(keys[i], vaules[i])); 
     } 
    } 

    /** 
    * Set the specified Collection at the array. 
    * 
    * @param keysVaules 
    */ 
    public void setKeyValue(final String[][] keysVaules) { 
     final int N = keysVaules.length; 
     for (int i = 0; i < N; i++) { 
      add(new KeyValue(keysVaules[i][0], keysVaules[i][1])); 
     } 
    } 

    private String[] entries; 
    private String[] entryValues; 

    /** 
    * Set the specified Collection at the array. 
    * 
    * @param entries 
    */ 
    public void setEntries(final String[] entries) { 
     this.entries = entries; 
     if (entryValues != null) { 
      setKeyValue(entryValues, entries); 
     } 
    } 

    /** 
    * Set the specified Collection at the array. 
    * 
    * @param entryValues 
    */ 
    public void setEntryValues(final String[] entryValues) { 
     this.entryValues = entryValues; 
     if (entries != null) { 
      setKeyValue(entryValues, entries); 
     } 
    } 

    /** 
    * Get the value of the KeyValue with the specified position in the data set. 
    * 
    * @param position 
    * @return 
    */ 
    public String getValue(final int position) { 
     return getItem(position).value; 
    } 

    /** 
    * Get the key of the KeyValue with the specified position in the data set. 
    * 
    * @param position 
    * @return 
    */ 
    public String getKey(final int position) { 
     return getItem(position).key; 
    } 

    /** 
    * Get the entry of the KeyValue with the specified position in the data set. 
    * 
    * @param position 
    * @return 
    */ 
    public String getEntry(final int position) { 
     return getValue(position); 
    } 

    /** 
    * Get the entry value of the KeyValue with the specified position in the data set. 
    * 
    * @param position 
    * @return 
    */ 
    public String getEntryValue(final int position) { 
     return getKey(position); 
    } 

} 
関連する問題