Home > OS >  Json does not go to next or previous text when clicking on button with using Linq in C#
Json does not go to next or previous text when clicking on button with using Linq in C#

Time:11-09

In the steps from my json file, I want to construct a linq that moves to the next or previous text depending on whether you hit the back button (previous id) or the next button (next contentid). If this occurs, the label's text must be modified. I'm not sure how to accomplish that in Linq.

The problem now is when I click the button the first time the text is empty. And then he shows the same text! What am I doing wrong?

Buttons' Click handlers

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class StepView : ContentPage
{
    //get step
    private Step _step;

    //go to selected step
    public StepView(Step step, string nextTitle)
    {
        _step = step;
        InitializeComponent();
        Title = nextTitle;
        // get label text
        lblText.Text = step.Contents.FirstOrDefault(x => x.Text != null).Text;
    }

    //go back to home
    public void btnHome_Clicked(object sender, EventArgs e)
    {
        //gaat naar de mainpage
        Navigation.PushAsync(new MainPage());
    }

    private int index;

    public void BtnNext_Clicked(object sender, EventArgs e)
    {
        index  ;
        // If the index go after the last element
        if (index >= _step.Contents.Count)
        {
            // Then reset
            index = 0;
        }
        var content = _step.Contents.ElementAtOrDefault(index);
        lblText.Text = content?.Text;
    }

    public void BtnBack_Clicked(object sender, EventArgs e)
    {
        index--;
        // If the index go before the first element
        if (index < 0)
        {
            // Then go to the last
            index = _step.Contents.Count - 1;
        }
        var content = _step.Contents.ElementAtOrDefault(index);
        lblText.Text = content?.Text;
    }

Domain classes

 public class RootObject
{
    [JsonProperty("protocols")]
    public List<Protocol> Protocols { get; set; }
}

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

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("steps")]
    public List<Step> Steps { get; set; }
}

public class Step
{
    [JsonProperty("chapterTitle")]
    public string ChapterTitle { get; set; }

    [JsonProperty("contents")]
    public List<Content> Contents { get; set; }
}

public class Content
{
    [JsonProperty("contentid")]
    public string Contentid { get; set; }

    [JsonProperty("text")]
    public string Text { get; set; }
}

The json file

{
  "protocols": [
    {
      "id": "1",
      "name": "Pols meten",
      "steps": [
        {
          "chapterTitle": "Voorzorg",
          "contents": [
            {
              "contentid": "1",
              "text": "voor blabla"
            },
            {
              "contentid": "2",
              "text": "voor blabla2"
            },
            {
              "contentid": "3",
              "text": "voor blablabla3"
            }
          ]
        },
        {
          "chapterTitle": "Handeling",
          "contents": [
            {
              "contentid": "4",
              "text": "Handeling blabla"
            },
            {
              "contentid": "5",
              "text": "Handeling blabla2"
            },
            {
              "contentid": "6",
              "text": "Handeling blybli3"
            }
          ]
        },
        {
          "chapterTitle": "Nazorg",
          "contents": [
            {
              "contentid": "7",
              "text": "Nazorg blabla"
            },
            {
              "contentid": "8",
              "text": "Nazorg blabla2"
            },
            {
              "contentid": "9",
              "text": "Nazorg blybli3"
            }
          ]
        }
      ]
    }
  ]
}

What i want is show the text from the next or previous content id based where you clicked on.

CodePudding user response:

Each step has ONE content :

...
"steps": [
  {
    "chapterTitle": "Voorzorg",
    "contents": [
      {
        "contentid": "1",
        "text": "voor blabla"
      }
    ]
  },
  ...

First click on the next button that display nothing :

// Initialized on the first step's content (I think)
private Content content;
public void BtnNext_Clicked(object sender, EventArgs e)
{
    int index;
    if(content == null)
    {
        index = 0;
    }
    else // Go here, because content is the first step's content (that is not null)
    {
        index = _step.Contents.IndexOf(content)
        // index <= 0
        index = index   1;
        // index <= 1
    }
    content = _step.Contents.ElementAtOrDefault(index);
    // content <= _step.Contents.ElementAtOrDefault(1);
    // content <= the second step's content
    // But the step has ONE content, then ElementAtOrDefault return null
    // content <= null
    lblText.Text = content?.Text;
    // Display nothing
}

Second click on the next button :

// After the first click, content is null
private Content content;
public void BtnNext_Clicked(object sender, EventArgs e)
{
    int index;
    if(content == null) // Go here, because content is null
    {
        index = 0;
    }
    else 
    {
        index = _step.Contents.IndexOf(content)   1;
    }
    content = _step.Contents.ElementAtOrDefault(index);
    // content <= _step.Contents.ElementAtOrDefault(0);
    // content <= the first step's content
    lblText.Text = content?.Text;
    // Redisplay the first step's content
}

You can keep the index (rather than the content) and check that it is in the range (rather than the content is not null) :

private int index;
public void BtnNext_Clicked(object sender, EventArgs e)
{
    index  ;
    // If the index go after the last element
    if(index >= _step.Contents.Count)
        // Then reset
        index = 0;
    var content = _step.Contents.ElementAtOrDefault(index);
    lblText.Text = content?.Text;
}



public void BtnBack_Clicked(object sender, EventArgs e)
{
    index--;
    // If the index go before the first element
    if(index < 0)
        // Then go to the last
        index = _step.Contents.Count - 1;
    var content = _step.Contents.ElementAtOrDefault(index);
    lblText.Text = content?.Text;
}

And add more content in the steps (else this loop on the unique step's content) :

"steps": [
  {
    "chapterTitle": "Voorzorg",
    "contents": [
      {
        "contentid": "1",
        "text": "voor blabla"
      },
      {
        "contentid": "2",
        "text": "vuur blabla"
      },
      {
        "contentid": "3",
        "text": "soor blybli"
      }
    ]
  },
  • Related