2014-10-01 193 views
0

我有點卡住了,需要一些關於如何使用從微調器中選擇的信息的想法。 第一微調是通過Web服務填充,通過此代碼對的AsyncTask:使用微調器的選定項目

@Override 
     protected Void doInBackground(Void... unused) { 
      ... 
      ... 
      ... 
      HttpTransportSE transportSE = new HttpTransportSE(URL); 
      try{ 
       //Web service call 
       transportSE.call(SOAP_ACTION_BRING_NEEDS, envelope); 

       //create SoapPrimitive and obtain response 
       resultsRequestSOAP = (SoapObject)envelope.getResponse(); 

       int intPropertyCount = resultsRequestSOAP.getPropertyCount(); 
       strNeeds = new String[intPropertyCount]; 

       for(int i= 0;i< intPropertyCount; i++) 
       {       
        //Format the information from the web service 
        Object property = resultsRequestSOAP.getProperty(i); 
        if (property instanceof SoapObject) { 
         SoapObject needsList = (SoapObject) property; 
         strNeeds[i] = needsList.getProperty("Descripcion").toString(); 
        } 
       } 

然後在postExecute我填的是微調調用方法spinnerNeeds():

public void spinnerNeeds() { 
     //Needs Spinner Control 
     spnNeeds = (Spinner)findViewById(R.id.spnNeeds); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strNeeds); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spnNeeds.setAdapter(adapter); 
     spnNeeds.setSelection(1); 

     spnNeeds.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       //Todo 
      } 
      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 
      } 
     }); 
    } 

的事情是, spinner由屬性(「Descripcion」)的strNeeds []填充,但Web服務響應有3個屬性,我需要另一個屬性調用「Codigo」,以便可以將信息發送給另一個活動。如何在微調器上選擇一個可以驗證我需要的「codigo」的選項後進行驗證?

我想創建另一個String [] strCode,並在FOR()中保存屬性「Codigo」的值,就像我對「Descripcion」屬性所做的一樣......但是如果我在Spinner上選擇一個選項,我驗證strCode?

PD:在「Codigo」屬性不包含崛起值,例如第一個值不是1,2,3,4 ....他們隨機值...

回答

0

答案將是相當複雜,但一旦你明白它非常簡單。 首先,您需要創建一個對象,說明需要3個屬性(說明,Codigo ..) 接下來,不是使用類型爲String的arrayadapter,而是使用NEED類型的arrayadapter。 接下來,在您的自定義類NEED中,重寫toString()方法,使其返回需要在微調器中顯示的屬性的值。 現在,選定的項目將返回NEED對象,從中獲得'Codigo'屬性的值。

class Need{ 
String description, codigo, property3; 
public Need(String desc, String codigo, String prop3) 
{ 
this.description = desc;//similarly for other 2 properties 
} 
@override 
private String toString() 
{ 
return this.description; 
} 
} 

strNeeds = new Need[intPropertyCount]; 
for(){ 
strNeeds[i] = new Need(); 
} 

ArrayAdapter<Need> adapter = new ArrayAdapter<Need>(this,android.R.layout.simple_list_item_1,  strNeeds); 
相關問題