Here is my code to bind data in spinner with key(ProductName) and value(ProductID). But I am unable to get the value OnItemSelect.
public class BindDropDown {
private String name;
private String id;
public String getName() {
return name;
}
public void setNames(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String toString() {
return (name);
}
}
and my android code:
public class SageActivity extends Activity implements OnItemSelectedListener {
public final String SOAP_ACTION = "http://tempuri.org/PageLoadEvent ";
public final String OPERATION_NAME = "PageLoadEvent ";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ADDRESS = "http://192.168.7.106:90/MyWebserviceProfile/Service1.asmx";
private static final String Cuspr_CustomerProductID = "Cuspr_CustomerProductID";
private static final String ProdName = "ProdName";
private EditText text1;
private Button button;
private Spinner spinnerProduct;
Object item = null;
JSONObject jsonObj = null;
// you can use this array to find the school ID based on name
ArrayList<BindDropDown> productList = new ArrayList<BindDropDown>();
// you can use this array to populate your spinner
ArrayList<String> productNames = new ArrayList<String>();
ArrayAdapter<BindDropDown> spinnerArrayAdapter = null;
// contacts JSONArray
JSONArray custProduct = null;
BindDropDown cp = new BindDropDown();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sage);
text1 = (EditText) findViewById(R.id.editText1);
spinnerProduct = (Spinner) findViewById(R.id.my_spinner);
spinnerProduct.setOnItemSelectedListener(this);
ConnectWS();
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text1 = (EditText) findViewById(R.id.editText1);
String txtID1 = spinnerProduct.getItemAtPosition(
spinnerProduct.getSelectedItemPosition()).toString();
}
});
}
public void ConnectWS() {
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);
PropertyInfo pi = new PropertyInfo();
pi.setName("description");
pi.setValue(text1.getText().toString());
pi.setType(String.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
httpTransport.debug = true;
String ss = httpTransport.responseDump;
// Log.d("Result --- ", ss);
Object response = null;
SoapPrimitive result = null;
try {
httpTransport.call(SOAP_ACTION, envelope);
if (envelope.bodyIn instanceof SoapFault) {
String str = ((SoapFault) envelope.bodyIn).faultstring;
Log.i("", str);
} else {
SoapObject so = (SoapObject) envelope.bodyIn;
Log.d("WS", String.valueOf(so));
}
if (envelope.getResponse() != null) {
result = (SoapPrimitive) envelope.getResponse();
String json = result.toString();
jsonObj = (JSONObject) new JSONTokener(json).nextValue();
custProduct = jsonObj.getJSONArray("CustomerProduct");
for (int i = 0; i < custProduct.length(); i++) {
JSONObject jsonObject = custProduct.getJSONObject(i);
cp.setNames(jsonObject.optString("ProdName"));
cp.setId(jsonObject.getString(Cuspr_CustomerProductID));
productList.add(cp);
productNames.add(jsonObject.optString("ProdName"));
}
spinnerProduct.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item,
productNames));
} else { // uh oh response is null
}
// SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
} catch (Exception exception) {
response = exception.toString();
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
Object test123 = spinnerProduct.getAdapter().getItem(position);
// selection.setText(items[position]);
// String test123 = jsonObject.getString(Cuspr_CustomerProductID);
String txtID = cp.getId();
text2.setText(txtID);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sage, menu);
return true;
}
}
Any workaround I need to do get the values from the spinner..?
Regards
Raja M
as you have productList, as Global variable to the Activity class, you can always access this list, so in onItemSelected get item of this list, and get id, and name from this item, as follows:
Edit:
Change loop in parsing to following:
as the same object is being added to list repetatively.