简体   繁体   中英

Json array to Picker in Xamarin Forms

First of all Im scanning the qr code with the application, then the app is making api request and I get json in my response from my server..

I want to get a json from my api and display it in picker in xaml, let me show you my json

[{"srednica":"20","silowniki":[{"stroke":"25","id":"1222","price":"118.00"},{"stroke":"40","id":"1224","price":"121.60"},{"stroke":"75","id":"1542","price":"130.00"},{"stroke":"230","id":"1545","price":"167.20"},{"stroke":"275","id":"1546","price":"178.00"}]},{"srednica":"16","silowniki":[{"stroke":"125","id":"1223","price":"116.00"},{"stroke":"150","id":"1225","price":"119.00"},{"stroke":"80","id":"1537","price":"110.60"},{"stroke":"120","id":"1538","price":"115.40"}]},{"srednica":"25","silowniki":[{"stroke":"25","id":"1247","price":"126.75"},{"stroke":"180","id":"1556","price":"168.60"},{"stroke":"185","id":"1557","price":"169.95"},{"stroke":"220","id":"1558","price":"179.40"}]},{"srednica":"12","silowniki":[{"stroke":"40","id":"1373","price":"99.00"},{"stroke":"150","id":"1533","price":"110.00"},{"stroke":"200","id":"1534","price":"115.00"}]},{"srednica":"10","silowniki":[{"stroke":"10","id":"1384","price":"92.00"},{"stroke":"30","id":"1577","price":"94.00"}]}]

Where "20" is the diameter and must be unique value that have to be in the 1st picker while in the 2nd picker I need to have stroke, id and price to that corrensponding diameter so for example:

I pick diameter = 20 and in 2nd picker I get all stroke's from that diameter ((loop)dia["20"][i].stroke <- thats how I would do it in javascript..).

Here is my model which I got from here https://app.quicktype.io/

public class Diameter : INotifyPropertyChanged
{
    [JsonProperty("srednica")]
    public string Srednica { get; set; }

    [JsonProperty("silowniki")]
    public List<Silowniki> Silowniki { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class Silowniki
{
    [JsonProperty("stroke")]
    public string Stroke { get; set; }

    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("price")]
    public string Price { get; set; }
}

My viewmodel

    public class ScannedViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<Silowniki> Silowniki { get; set; }
        public ObservableCollection<Diameter> Dia { get; set; }
        public Diameter SelectedDiameter { get; set; }
        public Diameter SelectedSilowniki { get; set; }
        public ScannedViewModel()
        {
            Silowniki = new ObservableCollection<Silowniki>();
            Dia = new ObservableCollection<Diameter>();
}

My json response here:

        var responseText = streamReader.ReadToEnd();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            return responseText;

And here is how I add the json to diameter, I doubt its the right way..

ScannerPage.OnScanResult += (result) =>
            {
                ScannerPage.IsScanning = false;
                Device.BeginInvokeOnMainThread(async () =>
                {
                    await Navigation.PopAsync();
                    dynamic jsonRespone = await ConnectWithOauth.GetRequest(result.Text);
                    var test = JsonConvert.DeserializeObject<List<Diameter>>(jsonRespone);
                    ScannedProducts nextPage = new ScannedProducts(test);
                    //nextPage.BindingContext = viewModel;

                    Console.WriteLine("Mydia pomyslnie init");
                    await Navigation.PushAsync(nextPage);

And lastly here is my xaml.

    <Picker Title="Test"
            x:Name="picker"
            ItemDisplayBinding="{Binding Silowniki.Id}">
    </Picker>
    <Picker Title="test"
            x:Name="kupa"
            ItemDisplayBinding="{Binding Silowniki.Stroke}">
    </Picker>
    <ListView 
        x:Name="ListaProduktow"  
        CachingStrategy="RecycleElement"
        RowHeight="60"
        ItemSelected="OnItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ContentView>
                        <Label Text="{Binding Srednica}">
                        </Label>
                    </ContentView>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Im binding to picker here in my "ScannedProducts" page:

    public ScannedProducts(dynamic test)
    {
        InitializeComponent();
        picker.ItemsSource = test;
        kupa.ItemsSource = test;
        BindingContext = test;

Please help me Im stuck in this for past 2 days and I really dont know what to do next...

Edited this message 5.03

What I want to achieve: Get "srednica" in 1 picker and whenever someone selects for example "20" to show him stroke in picker 2nd corrensponding to that "srednica" in json

My problem: it is showing me "srednica" but its not showing me stroke and id.. I have tried with picker's (commenting out listview) but I get java error "Java.Lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' "

I know JSON is properly deserializing into the model because I have checked, the elements are there, I think I dont know how to show it in picker.

Tell me what I need to do to do what I want to achieve?

Edit:

If I do in my Page.cs:

    kupa.ItemsSource = model[0].Silowniki;

And in my xaml:

<Picker Title="test"
        x:Name="kupa"
        ItemDisplayBinding="{Binding Stroke}">
</Picker>

It does show me stroke, but only for the element 0 that is :) How do I do it this way: "Picker 1: select srednica "20" if its selected show stroke from silowniki (model[thisSrednicaIndex].Silowniki.Stroke) for picker 2nd"

Or I have to do it in loop behind Page code?

Unfortunately, you don't understand some basic things and it is normal that you are learning them in multiple days.

The first thing that I can note is that you don't understand how data binding is working as you don't have the fields that you bind to in your view model at all. It could be that this is the complete answer, but probably there are other problems too and simply you'll have to learn the things, no one can do that for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM