[android]错误虽然接收数据的 Web 服务和绑定到 Spinner 与AsyncTask

标签: Asp.net Android
发布时间: 2017/3/17 20:38:18
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试着向接收数据从 Web 服务方法,返回 list (of string) 已经创建它在 ASP.NET 喜欢这

<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

像这样调用结果

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

现在我需要接受此数据并把它放在Spinner

我愿意。。。

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

在这条线还有一个错误

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

onPostExecute 方法

你能帮我做到这一点......

解决方法 1:

构造函数 ArrayAdapter (MainActivity.MyTask,int,String[]) 未定义

在下面这行︰

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

this不指 MyTask 类, Context 的你 Activity 像你的期望。您需要传递一个有效的引用 Context ,例如︰

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

另外

如何从一样的两个维度中检索列表

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

我需要显示在名称 spinner 和当我单击在一些项目的价值......

一种方法是使用 List<Map<String, String>> values 来存储每个 namevalue 在一张地图和显示您 name 字符串中 SpinnerSimpleAdapter :

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

最后里面你 onItemSelected() 方法现在,您可以使用 value 值而不是 name 值。

赞助商