2

这是我将微调器中的数据与键(产品名称)和值(产品ID)绑定的代码。但我无法获得 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);
    }

}

和我的安卓代码:

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;
    }

}

我需要从微调器中获取值的任何解决方法..?

问候拉贾 M

4

1 回答 1

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"));
                }

因为同一个对象被重复添加到列表中。

于 2012-10-29T11:48:45.307 回答