这是我在spinner中使用key(ProductName)和value(ProductID)绑定数据的代码。但是我无法获得值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);
}
}
和我的android代码:
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;
}
}
我需要做的任何解决方法都是从微调器获取值??
此致 Raja M
答案 0 :(得分:1)
因为你有productList,作为Activity类的全局变量,你总是可以访问这个列表,所以在onItemSelected获取这个列表的项目,并获得这个项目的id和名称,如下所示:
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
BindDropDown item= productList.get(position);
String txtID = item.getId();
text2.setText(txtID);
}
编辑:
将解析中的循环更改为以下内容:
for (int i = 0; i < custProduct.length(); i++) {
JSONObject jsonObject = custProduct.getJSONObject(i);
BindDropDown cp=new BindDropDown();
cp.setNames(jsonObject.optString("ProdName"));
cp.setId(jsonObject.getString(Cuspr_CustomerProductID));
productList.add(cp);
productNames.add(jsonObject.optString("ProdName"));
}
因为相同的对象被重复地添加到列表中。