Loai
Loai

Reputation: 732

Error While Receiving A Data From Web-Service And Bind them To Spinner With AsyncTask

i am Try To Receive A Data From A Web-Service Method That return a list (of string)I've Create it in ASP.NET Like This

<WebMethod()> _
Public Function HelloWorld() As List(Of String)
    Dim names As New List(Of String)
    names.Add("aaa")
    names.Add("bbb")
    names.Add("ccc")
    Return names
End Function

the invoke Result like this

<string>aaa</string>
<string>bbb</string>
<string>ccc</string>

Now I need to receive This Data And put it in the Spinner

I DO ...

public class MainActivity extends Activity 
{

private Spinner myspinner;
private static final String METHOD_NAME = "HelloWorld";  
private static final String NAMESPACE = "http://tempuri.org";   
private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String URL = "http://www.xxxxxxxxxxx.com/ws/AndroidMethods.asmx";
private String[] Values;

private void SektorDoldur()
{
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;     
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    androidHttpTransport.debug = true;
try 
    {
       androidHttpTransport.call(SOAP_ACTION, envelope);
       SoapObject response = (SoapObject) envelope.getResponse();
       Values = new String[response.getPropertyCount()];
       for(int i=0;i<response.getPropertyCount();i++)
          {     
           Values[i] = response.getPropertyAsString(i).toString();
          }      
    }
catch (Exception e)
    {
    e.printStackTrace(); 
    }
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myspinner = (Spinner)findViewById(R.id.spinner1);
    new MyTask().execute();
}
public class MyTask extends AsyncTask<Void, Void, String>
{

String response = "";
public void onPreExecute() 
  {
    super.onPreExecute();
  }
    @Override
protected String doInBackground(Void... arg0)
{
       SektorDoldur();
       return "";
}
@Override
public void onPostExecute(String res)
{

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, Values);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    myspinner.setAdapter(adapter);           
}
}
}

there is an error in this Line

ArrayAdapter<String> adapter = new ArrayAdapter<String>this,android.R.layout.simple_spinner_item, Values);

in the onPostExecute Method

Can You Help Me To Do That ...

Upvotes: 0

Views: 559

Answers (1)

Sam
Sam

Reputation: 86948

The constructor ArrayAdapter(MainActivity.MyTask, int, String[]) is undefined

In this line:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Values);

this is referring to the MyTask class, not the Context of your Activity like you are expecting. You need to pass a reference to a valid Context, for example:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, Values);

Addition

HOW Can I Retrieve a list from two dimension like

Public Class NameAndvalue
    Public name As String
    Public value As String
End Class

I need to show the name in the spinner and the value when i click at some item ...

One way is to use List<Map<String, String>> values to store each name and value in a Map and display your name Strings in the Spinner with a SimpleAdapter:

SimpleAdapter adapter = new SimpleAdapter(this, values, 
        android.R.layout.simple_spinner_item, 
        new String[] {"name"}, new int[] {android.R.id.text});

Lastly inside your onItemSelected() method you can now use the value value instead of the name value.

Upvotes: 2

Related Questions