0
我是Android新手。我正在嘗試從Web服務的數組列表中填充列表視圖。我一直沒有找到任何使用數組到listview的在線教程。我令牌「最終」,無效的類型得到一個語法錯誤如何從Web服務設置列表視圖陣列
這裏是我的Java代碼:
public class LocationFragment extends Fragment {
ListView tv;
public LocationFragment() {
}
private String TAG = "Vik";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_location, container, false);
tv = (ListView) rootView.findViewById(R.id.listView1);
AsyncCallWS task = new AsyncCallWS();
task.execute();
return rootView;
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
location();
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG, "onPostExecute");
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG, "onProgressUpdate");
}
}
public void location()
{
String SOAP_ACTION = "http://example.com/getlocations";
String METHOD_NAME = "getlocations";
String NAMESPACE = "http://example.com/";
String URL = "http://localhost/example/Service.asmx";
try {
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
Request.addProperty("get locations", "null");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
HttpTransportSE transport= new HttpTransportSE(URL);
transport.call(SOAP_ACTION, envelope);
Object result=(Object)envelope.getResponse();
final results = (String[]) result;
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// This code will always run on the UI thread, therefore is safe to modify UI elements.
ArrayAdapter<MyObject> adapter = new ArrayAdapter<MyObject>(this,
android.R.layout.simple_list_item_1, results);
tv.setAdapter(adapter);
}
});
}
catch(Exception ex) {
Log.e(TAG, "Error: " + ex.getMessage());
}
}
}
而且,這裏是我的web服務的任何幫助將不勝感激。
[WebMethod]
public string[] getlocations()
{
SqlConnection cn = new SqlConnection("Data Source=DEV-SQL1;Initial Catalog=portal;User Id=wstest;Password=wstest;");
List<string> locations = new List<string>();
cn.Open();
string sqlquery = string.Format("SELECT LocationName FROM ts_locations where CompanyID = '130' ORDER by LocationName");
SqlCommand com = new SqlCommand(sqlquery, cn);
SqlDataReader sqlReader = com.ExecuteReader();
while (sqlReader.Read())
{
locations.Add(sqlReader.GetString(0));
}
cn.Close();
return locations.ToArray();
}