0

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?

6
  • Your JSON seems like not a valid JSON. There are missing closing bracket and excess comma. Commented Nov 27, 2019 at 2:46
  • @Hermanto I have fixed the json in the post above Commented Nov 27, 2019 at 2:55
  • It seems you didn't set ItemsSource of ListView ListTryout? Commented Nov 27, 2019 at 5:43
  • I have replaced ItemsSource = {Binding TryoutList}, but it still cannot display "tryout_terbaru" list Commented Nov 27, 2019 at 6:00
  • Could you add to question Highlight and TryoutList classes? Commented Nov 27, 2019 at 6:08

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.