string num;
int num2;
num = "2";
// method 1 - will throw Exception if num is not a parsable int
num2 = int.Parse(num);
// method 2 - will not throw exception - if success is true num2 will contain the parsed int
bool success = int.TryParse(num,out num2);
Callling a wcf within android app and passing listview selected item
Question
I know I posted a question the other day about how should I be passing a selected android listview item to the a wcf serivce application. I am just trying to use the simplest approach to get things working. I am having the problem of getting the towns from the selected county using a wcf. I have a county table with id and county. In a towns table I have id, name and countyid.
Here is my wcf service application:
[OperationContract]
Festivalwrapper GetTownDataByCounty(int? id);
public Festivalwrapper GetTownDataByCounty(int? id)
{
Festivalwrapper returnType = new Festivalwrapper();
using (azureDBDataContext c = new azureDBDataContext())
{
returnType.TownList = (from town in c.Towns
where town.CountyID.Equals(id)
select new TownVM()
{
ID = town.ID,
Name = town.Name,
}).ToList();
}
return returnType;
}
Here is my OnItemClick
for my first activity:
public void OnItemClick(AdapterView parent, View view, int position, long id)
{
var selectedValue = parent.GetItemIdAtPosition(position);
//InitializeDataTownById(int position);
var Intent = new Intent(this, typeof(SelectLocationActivity));
// selectedValue should already be a string but...
Intent.PutExtra("Name", selectedValue.ToString());
StartActivity(Intent);
}
And my second activity:
[Activity(Theme = "@style/Theme.AppCompat.Light")]
public class SelectLocationActivity : Activity
{
private DataTransferProcClient _client;
TextView _getTownsTextView;
ListView _listView;
public static readonly EndpointAddress EndPoint = new EndpointAddress("http://10.0.2.2:3190/DataTransferProc.svc");
//private int id;
protected override void OnCreate(Bundle bundle)
{
//var id = Intent.GetStringExtra("position");
base.OnCreate(bundle);
SetContentView(Resource.Layout.selectLocation);
_listView = FindViewById<ListView>(Resource.Id.lvTowns);
//_listView.OnItemClickListener = this;
_listView.FastScrollEnabled = true;
_getTownsTextView = FindViewById<TextView>(Resource.Id.getTownsTextView);
InitializeDataTownById();
}
#region InitializeDataTown
private void InitializeDataTownById()
{
var id = Intent.GetStringExtra("Name");
int value;
int.TryParse(id, out value);
if (id != null)
{
BasicHttpBinding binding = CreateBasicHttp();
_client = new DataTransferProcClient(binding, EndPoint);
_client.GetTownDataByCountyCompleted += ClientOnDataTransferProcCompleted;
_client.GetTownDataByCountyAsync(id);
}
//_client.Close ();
}
#endregion
#region CreateBasicHttp
private static BasicHttpBinding CreateBasicHttp()
{
var binding = new BasicHttpBinding()
{
Name = "basicHttpBinding",
MaxReceivedMessageSize = 67108864,
};
binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxArrayLength = 2147483646,
MaxStringContentLength = 5242880,
};
var timeout = new TimeSpan(0, 1, 0);
binding.SendTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
return binding;
}
#endregion
#region ClientOnDataTransferProcCompleted
//test connection to wcf, if doesn't work, you'll get an error
private void ClientOnDataTransferProcCompleted(object sender, GetTownDataByCountyCompletedEventArgs getTownDataByCountyCompletedEventArgs)
{
string msg = null;
if (getTownDataByCountyCompletedEventArgs.Error != null)
{
msg = getTownDataByCountyCompletedEventArgs.Error.Message;
msg += getTownDataByCountyCompletedEventArgs.Error.InnerException;
RunOnUiThread(() => _getTownsTextView.Text = msg);
}
else if (getTownDataByCountyCompletedEventArgs.Cancelled)
{
msg = "Request was cancelled.";
RunOnUiThread(() => _getTownsTextView.Text = msg);
}
else
{
msg = getTownDataByCountyCompletedEventArgs.Result.ToString();
TestAndroid.Festivalwrapper testHolder = getTownDataByCountyCompletedEventArgs.Result;
List<string> holder = testHolder.TownList.Select(item => item.Name).ToList();
/*foreach (TestAndroid.CountyVM item in TestHolder.CountyList)
{
holder.Add (item.Name);
}*/
RunOnUiThread(() => _listView.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, holder));
}
}
#endregion
I am also having a problem converting the string into an int. I am getting errors at : _client.GetTownDataByCountyAsync(id);
- Error CS1503: Argument 1: cannot convert from 'string' to 'int' (CS1503)
- Error CS1502: The best overloaded method match for 'DataTransferProcClient.GetTownDataByCountyAsync(int)' has some invalid arguments (CS1502)
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow