我有Spinner
,它從解析JSON中得到了一些項目。 JSON發送我的應用程序「statusId」和「statusTitle」。我可以如何設置我的JSON「statusID」爲「statusTitle」,因此在下一步我可以用我的「statusId」調用getSelectItem
?如何爲微調器中的項目分配ID?
getSelectItemId
不適合這種情況,因爲不同的「statusId」和「positioId」。
P.S.請不要拿我的問題是嚴格的,謝謝。
我有Spinner
,它從解析JSON中得到了一些項目。 JSON發送我的應用程序「statusId」和「statusTitle」。我可以如何設置我的JSON「statusID」爲「statusTitle」,因此在下一步我可以用我的「statusId」調用getSelectItem
?如何爲微調器中的項目分配ID?
getSelectItemId
不適合這種情況,因爲不同的「statusId」和「positioId」。
P.S.請不要拿我的問題是嚴格的,謝謝。
保存他們在不同的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有id 2等等。現在,每當你選擇任何項目。獲取它的位置並檢查該位置上的數組列表中的項目。你會得到兩個編號和標題
title.get(position of selected item);
id.get(position of selected item);
用類列表中微調您的項目:
public class Item {
public int id;
public String label;
@Override
public String toString() { return label; }
}
添加一個ArrayList或項目到您的ArrayAdapter一個數組,並讓你的適配器添加到微調。
擺脫微調所選項目:
Item item = (Item) spinner.getSelectedItem();
,並獲得ID與item.id
對不起,我的英語水平。
創建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;
}
}
,並使用自定義適配器填充此。
我創建了一個類來保存鍵值,然後創建一個適配器。
要填充你可以做這樣的事情微調:
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);
}
}