Reputation: 217
When I select any Item from spinner, I get position, Item Name. When I will click on any Item, I need this ItemId. What can I do?
I have used retrofit. I get a data list from webservice.
List<Zone> zonelst=response.body().getZoneslist();
zonelst contain zid and zname .
I have loaded "items"(zname) to the spinner. When I will click on any Items (zname), I need itemsid(zid).
for(int i=0; i<zonelst.size(); i++){
//Storing names to string array
items[i] = zonelst.get(i).getZname();
itemsid[i]=zonelst.get(i).getZid();
}
MODEL:
public class Zone {
@SerializedName("ZID")
private String zid;
@SerializedName("ZName")
private String zname;
public Zone(String zid, String zname) {
this.zid = zid;
this.zname = zname;
}
public String getZid() {
return zid;
}
public void setZid(String zid) {
this.zid = zid;
}
public String getZname() {
return zname;
}
public void setZname(String zname) {
this.zname = zname;
}
}
ACTIVITY:
ZoneApiInterface apiService=OperatingApiClient.getClient().create(ZoneApiInterface.class);
Call<ZoneApiResponse> call = apiService.getZoneInfoList();
call.enqueue(new Callback<ZoneApiResponse>() {
@Override
public void onResponse(Call<ZoneApiResponse> call, Response<ZoneApiResponse> response) {
List<Zone> zonelst=response.body().getZoneslist();
Log.d(TAG,"Number of Zone received1: "+zonelst.size());
//*********DROPDOWN******************************************************
//String array to store all the zone names
String[] items = new String[zonelst.size()];
String[] itemsid = new String[zonelst.size()];
//Traversing through the whole list to get all the names
for(int i=0; i<zonelst.size(); i++){
//Storing names to string array
items[i] = zonelst.get(i).getZname();
itemsid[i]=zonelst.get(i).getZid();
}
final Spinner spnrZone=(Spinner)findViewById(R.id.spinnerZone);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item, items); //getApplication()
//setting adapter to spinner
spnrZone.setAdapter(adapter);
//Creating an array adapter for list view
spnrZone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int Hold = spnrZone.getSelectedItemPosition()+1;
Toast.makeText(getApplicationContext(), "Item Position is = "+Hold, Toast.LENGTH_SHORT).show();
String N=parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), "Item Name = "+N, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onFailure(Call<ZoneApiResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG,t.toString());
}
});
Upvotes: 0
Views: 588
Reputation: 6823
spnrZone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int Hold = spnrZone.getSelectedItemPosition()+1;
Toast.makeText(getApplicationContext(), "Item Position is = "+Hold, Toast.LENGTH_SHORT).show();
String N=parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), "Item Name = "+N, Toast.LENGTH_SHORT).show();
String id=itemsid[position]; //simply get its id (String or int use whatev er its type)
}
use itemsid[position];
Upvotes: 1
Reputation: 163
I would recommend this answer if you want to stay with your original code.
But this is not optimised piece of code.
After getting the response you copy all the znames and zids to the array which takes extra space as well as time (For-loop).
I would recommend an alternate approach.
Override the toString
method in your Zone
model. Add this to your Zone
model :
@Override
public String toString() {
return getZname();
}
In you ACTIVITY :
Call<ZoneApiResponse> call = apiService.getZoneInfoList();
call.enqueue(new Callback<ZoneApiResponse>() {
@Override
public void onResponse(Call<ZoneApiResponse> call, Response<ZoneApiResponse> response) {
List<Zone> zonelst=response.body().getZoneslist();
Log.d(TAG,"Number of Zone received1: "+zonelst.size());
//*********DROPDOWN******************************************************
final Spinner spnrZone=(Spinner)findViewById(R.id.spinnerZone);
ArrayAdapter<Zone> adapter;
adapter = new ArrayAdapter<Zone>(getApplicationContext(), R.layout.simple_selectable_list_item,zonelst); //getApplication()
//setting adapter to spinner
spnrZone.setAdapter(adapter);
//Creating an array adapter for list view
spnrZone.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Zone z = (Zone) parent.getItemAtPosition(position);
int Hold = spnrZone.getSelectedItemPosition()+1;
Toast.makeText(getApplicationContext(), "Item Position is = "+Hold, Toast.LENGTH_SHORT).show();
String N = z.getZname();
String zid = z.getZid();
Toast.makeText(getApplicationContext(), "Item Name = "+N+",Item ID = "+zid, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onFailure(Call<ZoneApiResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG,t.toString());
}
});
Upvotes: 2