How To Bind the JSON Sub Array Data into Listview in xamarin.forms

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 04:02:32

问题


I want to bind the data coming from JSON to ListView in xamarin.forms. i have made the call and i have parsed the data from JSON. Now i am not able to understand the best way to bind the data in the xamarin.forms that is.

JSON:

[{"Product":"XXX","Origins":[{"Origin":"ORI1","Details":[{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}]},{"Origin":"ORI2","Details":[{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}]}]},{"Product":"PQR","Origins":[{"Origin":"ABC","Details":[{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}]},{"Origin":"PPP","Details":[{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"},{"Seller":"Name","Seller_Rate":"256","Saller_QTY":"20","Buyer":"Name","Buyer_Rate":"256","Buyer_QTY":"20"}]}]}]

this JSON has Product,Origins,Details such as product has many Origins and origins has different seller,buyer and etc.

my source is- my object class:

public class Details
{
    public string Seller { get; set; }
    public string Seller_Rate { get; set; }
    public string Saller_QTY { get; set; }
    public string Buyer { get; set; }
    public string Buyer_Rate { get; set; }
    public string Buyer_QTY { get; set; }
}
public class Origins
{
    public string Origin { get; set; }
    public List<Details> Details { get; set; }
}
public class Products
{
    public List<Origins> Origins { get; set; }
    public string PRODUCT { get; set; }
}

I have Parsed it inside this method to test result:

public async Task<List<Products>> getProductsJson()
    {
        List<Products> root=null;
        Log.Debug("Login", "in get products");
        string responsejson = await client.GetStringAsync(Constants.url_getproducts);
        Log.Debug("Login", responsejson);
        try
        {
            root = JsonConvert.DeserializeObject<List<Products>>(responsejson);
            Log.Debug("Login",Convert.ToString(root.Count));
            for(int i=0;i<root.Count;i++)
            {
                Log.Debug("Login", "=============================");
                Log.Debug("Login", root[i].PRODUCT);
                for(int j=0;j<root[i].Origins.Count;j++)
                {
                    Log.Debug("Login","   "+root[i].Origins[j].Origin);
                    for(int k=0;k<root[i].Origins[j].Details.Count;k++)
                    {
                        Log.Debug("Login", "       Buy " + root[i].Origins[j].Details[k].Buyer+" BUY_QTY "+ root[i].Origins[j].Details[k].Buyer_QTY);

                    }
                }
                Log.Debug("Login", "=============================");
            }
            //Log.Debug("Login", root[0].PRODUCT);
            //Log.Debug("Login", root[0].Origins[0].Origin);
           // Log.Debug("Login", root[0].Origins[0].Details[0].Seller);
        }
        catch(Exception e)
        {
            Log.Debug("Login", e.Message);
            Log.Debug("Login", e.Source);
        }
        return root;
    }

As i have printed the log here, this log prints properly up to here every thing is working fine. i have called this method on the xamal page as:

async void GetProduct()
    {
        List<Products> result = await App.RestService.getProductsJson();
        list_data.ItemsSource = result;
    }

here i have set the result to listview.

On Design page:

<ContentPage.Content>
    <StackLayout VerticalOptions="FillAndExpand">
        <RelativeLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
        <StackLayout x:Name="logoStack">
            <Label x:Name="lbl_noInternet" HorizontalTextAlignment="Center" TextColor="Red" BackgroundColor="Black" />
        </StackLayout>
        <ListView x:Name="list_data"  HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <!--<TextCell Text="{Binding Topic}" TextColor="Purple" Detail="{Binding Description}" DetailColor="Black"/>-->
                    <!--<ImageCell Text="{Binding Topic}" TextColor="Purple" Detail="{Binding Description}" DetailColor="Black" ImageSource="{Binding ImageSource}" />-->
                    <ViewCell>
                        <ViewCell.View>
                            <StackLayout Spacing="5" Orientation="Horizontal" HorizontalOptions="StartAndExpand" >
                                <Label Text="{Binding PRODUCT}" FontSize="20" FontAttributes="Bold" />
                                    <Label   Text="{Binding Origin}" FontSize="20" HorizontalOptions="End" HorizontalTextAlignment="End" />
                                </StackLayout>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        </RelativeLayout>
    </StackLayout>
</ContentPage.Content>

Now. my code is working fine that means no any error but only PRODUCT is displayed, Origin value is not showing on label.

Now i have 3 questions.

  1. as origin is the sub array how to bind it to the label inside listview?
  2. which is the best way to achieve this. this same approach or to bind it programmatically?

3.if we need to bind it programmatically means dynamically from .cs file how to do that??

来源:https://stackoverflow.com/questions/45635815/how-to-bind-the-json-sub-array-data-into-listview-in-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!