如何在微调器中为id分配id?

时间:2016-10-04 14:01:22

标签: android spinner android-spinner

我有Spinner,它有一些来自解析JSON的项目。 JSON在我的应用程序中发送“statusId”和“statusTitle”。如何将我的JSON“statusID”设置为“statusTitle”,以便在下一步中我可以使用“statusId”调用getSelectItem

getSelectItemId在这种情况下不合适,因为不同的“statusId”和“positioId”。

P.S。请不要严格对待我的问题,谢谢。

4 个答案:

答案 0 :(得分: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);

现在标题A的ID为1,B的标识为2,依此类推。现在每当你选择任何一个项目。获取它的位置并检查该位置的两个arraylist中的项目。你会得到身份和头衔

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

答案 1 :(得分:0)

使用class在微调器中列出您的项目:

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

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

将ArrayList或Item of Array添加到ArrayAdapter中,以便将适配器添加到微调器。

从微调器中获取所选项目:

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

并使用item.id

访问id

抱歉我的英文。

答案 2 :(得分:0)

使用ItemName和ItemId创建一个类,如下所示:

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;
   }
}

并使用自定义适配器填充此内容。

答案 3 :(得分:0)

我创建了一个类来保存键值,然后创建一个适配器。

要填充微调器,您可以执行以下操作:

String []

请注意,在此示例中,在资源中的array.xml中使用数组填充适配器。但是,如果愿意,可以填充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); } }

和类适配器 KeyValueArrayAdaper.java

{{1}}