Home > Back-end >  How to trim data from picker using xamarin
How to trim data from picker using xamarin

Time:02-15

I want to pass the part of data from picker like as parameter for api point.

So for now picker have two properties - Station number & Station name. I want to get first parameter (Station number) and pass to other method for api.

From this method I want to get only the first property from the picker:

ScreenShot

public async Task FillStation_HQ_AHSAsync()
    {
        var result = await _restServiceStations.Get_HQ_AHS(GenerateRequestUriStations(Constants.EndPoint));

        //List for stations
        var stationsList = new List<string>();

        foreach (var item in result)
        {
            //Adding stations in list
            stationsList.Add(item.Station.ToString()   " - "   item.Ime.ToString());
        }

        //Filling picker with stations
        picker.ItemsSource = stationsList;

        //Make global string for picked station
        string pickStation = picker.SelectedItem.ToString();
        getStationNumber = pickStation;

        //Displaying date in entry
        DateTime dateNow =  DateTime.Now;
        entryDate.Text = dateNow.ToString("yyyy-MM-dd");

        //Displaying days before in entry
        entryDaysBefore.Text = 7.ToString();

        //Make global date time variable
        daysBeforeData = entryDaysBefore.Text;
    }

And want to pass the parameter on this method in statNum:

string GenerateRequestUri2(string endpoint, string dateAPI, string num, int statNum)
    {
        DateTime dateNow = DateTime.Now;

        dateAPI = dateNow.ToString("yyyy-MM-dd");

        num = daysBeforeData;

        string requestUri = endpoint;
        requestUri  = "/hqdataahs/GetData?";
        requestUri  = $"date={dateAPI}&";
        requestUri  = $"num={num}&";
        requestUri  = $"statNum={statNum}";

        return requestUri;
    }

How it's possible to get only the first item with selectedItem and How to do ?

CodePudding user response:

you can substring the first part:

int numb= Int32.Parse(pickStation.Substring(0, pickStation.IndexOf("-")));

CodePudding user response:

this is basic C# string manipulation

// split string on spaces
var split = pickStation.Split(' ');

// get the first item
var id = split[0];
  • Related