0

I'm learning Xamarin and was trying to push this Jira Json output to a listview named ListView1. If anyone would like to help a newbie please by all means. Everything works up to outputting data in debug, when i output the content but does not go to the list view. What am I doing wrong?

Note I had to use DataContract for Avatar URLS because it was bringing up invalid when ran in json2sharp. not sure if thats correct.

Here is a sample of the Json OUTPUT https://0bin.net/paste/8vbm-XlwIPcuxCeL#qG-rL5BzxIzCNx/3LpUdO8VMukLuFiAvG7+wHdNbra5 which I made into a Class, (Working with Jira output)

public class ItemClass
    {
        public class Links
        {
            public string @base { get; set; }
            public string context { get; set; }
            public string self { get; set; }
        }

        public class CreatedDate
        {
            public string iso8601 { get; set; }
            public string jira { get; set; }
            public string friendly { get; set; }
            public object epochMillis { get; set; }
        }

        [DataContract]
        public class AvatarUrls
        {
            [DataMember(Name = "48x48")]
            public string Icon_48x48 { get; set; }
            [DataMember(Name = "24x24")]
            public string Icon_24x24 { get; set; }
            [DataMember(Name = "16x16")]
            public string Icon_16x16 { get; set; }
            [DataMember(Name = "32x32")]
            public string Icon_32x32 { get; set; }
        }

        public class Links2
        {
            public string jiraRest { get; set; }
            public AvatarUrls avatarUrls { get; set; }
            public string self { get; set; }
        }

        public class Reporter
        {
            public string name { get; set; }
            public string key { get; set; }
            public string emailAddress { get; set; }
            public string displayName { get; set; }
            public bool active { get; set; }
            public string timeZone { get; set; }
            public Links2 _links { get; set; }
        }

        public class RequestFieldValue
        {
            public string fieldId { get; set; }
            public string label { get; set; }
            public object value { get; set; }
        }

        public class StatusDate
        {
            public string iso8601 { get; set; }
            public string jira { get; set; }
            public string friendly { get; set; }
            public object epochMillis { get; set; }
        }

        public class CurrentStatus
        {
            public string status { get; set; }
            public StatusDate statusDate { get; set; }
        }

        public class Links3
        {
            public string web { get; set; }
            public string self { get; set; }
        }

        public class Value
        {
            public List<string> _expands { get; set; }
            public string issueId { get; set; }
            public string issueKey { get; set; }
            public string requestTypeId { get; set; }
            public string serviceDeskId { get; set; }
            public CreatedDate createdDate { get; set; }
            public Reporter reporter { get; set; }
            public List<RequestFieldValue> requestFieldValues { get; set; }
            public CurrentStatus currentStatus { get; set; }
            public Links3 _links { get; set; }
        }

        public class RootObject
        {
            public List<string> _expands { get; set; }
            public int size { get; set; }
            public int start { get; set; }
            public int limit { get; set; }
            public bool isLastPage { get; set; }
            public Links _links { get; set; }
            public List<Value> values { get; set; }
        }

    }

Code when executed to display:

using (HttpClient client = new HttpClient())
            {
                var content = "";
                string url = "https://***/rest/servicedeskapi/request";
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", baseauth);
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync(url);
                content = await response.Content.ReadAsStringAsync();

                var Items = JsonConvert.DeserializeObject<List<ItemClass.RootObject>>(content);
                ListView1.ItemsSource = Items;

Listview on xaml:

<ListView x:Name="ListView1" RowHeight="60">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Vertical" Padding="8,0,8,0">
                                <Label Text="{Binding reporter}" TextColor="#000" FontSize="14" LineBreakMode="TailTruncation" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
1
  • your ItemSource is a List<RootObject>, and your Label is bound to "reporter", but RootObject does not have a "reporter" property. Commented May 22, 2017 at 0:54

1 Answer 1

2

Your binding to a List<RootObject>. And in your Cell you bind to the reporter property. The RootObject however, does not have a reporter property. So either add the reporter to it or, looking at your code, make the ItemsSource: Items.values, which has the reporter property.

So change your code like this:

var Items = JsonConvert.DeserializeObject<List<ItemClass.RootObject>>(content);
ListView1.ItemsSource = Items.values;
Sign up to request clarification or add additional context in comments.

10 Comments

Ah I was under the impression that as it was deserializeing to an object list in side root, it would then just post all values in the class via Items. How do I go about getting the values/objects? like Items.values as it seems to be invalid for Items.values
Nope it will bind to the type that is in the list that you set the ItemsSource to and only the properties in that object are available to you
Oh It shed light but no alas my problem still not solved. Trying to figure out how to display the output as Items.values is not defined value for itemssource.
You mean the values property is null or? Also you can't use the reporter directly as it is a complex type. Try setting it to something like reporter.name or override the ToString of the Reporter class
Was just using reporter as a test, but im trying to push all the content received from the json output into the list view. none of the steps ive tried seem to help, I'll prob have to go look at some example PCL's and try out some things.
|

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.