我尝试从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
方法中的
你能帮助我做到吗......
答案 0 :(得分:2)
构造函数ArrayAdapter(MainActivity.MyTask,int,String [])未定义
在这一行:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Values);
this
指的是MyTask类,而不是您期望的Activity的Context。您需要传递对有效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
我需要在微调器中显示名称,并在点击某个项目时显示值...
一种方法是使用List<Map<String, String>> values
将每个name
和value
存储在地图中,并使用SimpleAdapter在Spinner中显示name
字符串:
SimpleAdapter adapter = new SimpleAdapter(this, values,
android.R.layout.simple_spinner_item,
new String[] {"name"}, new int[] {android.R.id.text});
最后,在onItemSelected()
方法中,您现在可以使用value
值而不是name
值。