2

これは、スピナーのデータを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;
    }

}

スピナーから値を取得するために必要な回避策はありますか?

よろしくラジャ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 に答える