简体   繁体   中英

Display listview in listview from json

I have a JSON from the server to display in listview, as below:

 {
    "data": {
    "tryout_terbaru": {
            "title": "Ada Soal tryout baru lho! Coba kerjain yuk!",
            "list": [
                {
                    "id": "1173",
                    "judul": "SD kelas 3 - Latihan Pembagian (9)"
                }
            ],
            "tipe": "Tryout"
        }
        }

I want to display "tryout_terbaru" list in listview.

XAML:

<ListView
    x:Name="highlightListview"
    DataContext="{Binding SelectedItem, ElementName=itemListView}"
    ItemsSource="{Binding Source={StaticResource itemsViewSource}}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <ListView
                        x:Name="ListTryout"
                        DataContext="{Binding SelectedItem, ElementName=itemListView}"
                        ItemsSource="{Binding TryoutList">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <TextBlock
                                        Margin="0,15,15,10"
                                        Text="{Binding TJudul}" />
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

Code:

ObservableCollection<Highlight> highlightDatasource = new ObservableCollection<Highlight>();
ObservableCollection<TryoutList> tryoutDatasource = new ObservableCollection<TryoutList>();
JsonObject tryoutBObject = jsonData.ContainsKey("tryout_terbaru") && jsonData["tryout_terbaru"] != null ? jsonData["tryout_terbaru"].GetObject() : JsonObject.Parse("");
try
{
    title = tryoutBObject["title"].GetString();
    JsonArray JsonList = tryoutBObject["list"].GetArray();
    foreach (JsonValue groupValue in JsonList)
    {
        JsonObject groupObject = groupValue.GetObject();
        tryoutTitle = groupObject["judul"].GetString();
        TryoutList list = new TryoutList();
        list.TJudul = list;
        tryoutDatasource.Add(list);
    }
    Highlight highlightTB = new Highlight();
    highlightTB.Title = title;
    highlightDatasource.Add(highlightTB);
    highlightListview.ItemsSource = highlightDatasource;
}

I'm having a problem, which is not being able to display "tryout_terbaru" list in listview. How to handle it?

I assume that you have TryoutList in Highlight as following, if not you have to add this property.

public class Highlight
{
    public string Title { get; set; }
    public ObservableCollection<TryoutList> TryoutList { get; set; }
    public string Tipe { get; set; }
}

Then you need to set TryoutList that it seems you missed.

title = tryoutBObject["title"].GetString();
JsonArray JsonList = tryoutBObject["list"].GetArray();
foreach (JsonValue groupValue in JsonList)
{
    JsonObject groupObject = groupValue.GetObject();
    tryoutTitle = groupObject["judul"].GetString();
    TryoutList list = new TryoutList();
    list.TJudul = list;
    tryoutDatasource.Add(list);
}
Highlight highlightTB = new Highlight();
highlightTB.Title = title;
highlightTB.TryoutList = tryoutDatasource;//You Missed this part
highlightDatasource.Add(highlightTB);
highlightListview.ItemsSource = highlightDatasource;

Finally TryoutList must be shown by ItemsSource="{Binding TryoutList">

You can also check WPF nested ListView ItemsSource

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