1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
if (response == null || response.length() < 10) {
progress.dismiss();
return;
}
String resp = response;
JSONArray clients = null;
try {
clients = new JSONArray(resp);
if (clients == null || resp.length() < 10)
return;
JSONObject client_obj;
SpinnerItem client = null;
ArrayList<SpinnerItem> clients_LIST=new ArrayList<SpinnerItem>();
//add dummy record
client = new SpinnerItem("0","");
for (int i = 0; i < clients.length(); i++) {
client_obj = (JSONObject) clients.get(i);
client = new SpinnerItem(String.valueOf(client_obj.getInt("id")),client_obj.getString("client_name"));
clients_LIST.add(client);
}
//add to spinner
ArrayAdapter<SpinnerItem> dataAdapter = new ArrayAdapter<SpinnerItem>(clients_Map.this, android.R.layout.simple_spinner_item, clients_LIST);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
actionbar_spinner.setAdapter(dataAdapter);
} catch (JSONException e) {
General.mes(Clients_Map.this, e.getMessage());
} finally {
progress.dismiss();
}
.
.
.
.
//add to actionbar + create click event
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// setup on actiobar - http://stackoverflow.com/a/30639393/1320686
getMenuInflater().inflate(R.menu.clients_map, menu);
MenuItem item = menu.findItem(R.id.map_spinner);
actionbar_spinner = (Spinner) MenuItemCompat.getActionView(item);
get_clients();
actionbar_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos==0)
return;
SpinnerItem client= (SpinnerItem)parent.getItemAtPosition(pos);
Toast.makeText(Clients_Map.this, client.getTitle() + " with id " + client.getId(),Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}); // (optional)
return true;
}
//R.menu.clients_map.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/map_spinner"
android:actionViewClass="android.widget.Spinner"
android:icon="@drawable/ic_launcher"
android:orderInCategory="1"
android:showAsAction="always"
android:title="pipiscrew"/>
</menu>
origin - http://www.pipiscrew.com/?p=4851 android-set-json-records-to-spinner