0

the app consists of a listview that populates from an api that returns a list of surveys for a user to complete. the listview display the id, type and date. the user is to click on that and be taken to a screen to complete to survey.

-what I am trying to do is store the id that is fetched and displayed in the list view row and pass it to the next screen in the intent. so that every time a user clicks the item in the list the id is passed to the next screen when completing the survey automatically and the data is entered back to the api to the correct survey based on the survey ID. I currently am statically passing the survey ID across so can only complete one survey.

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Newtonsoft.Json;

namespace Dribl.Droid
{
    [Activity(Label = "Surveys", Theme = "@style/CustomActionBarTheme")]
    public class Surveys : Activity
    {
        LinearLayout surveysBtn;
        LinearLayout availabilityBtn;
        LinearLayout inboxBtn;
        LinearLayout dashboardBtn;

        //Button backBtn;

        private List<String> surveys;
        private ListView surveyListview;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Surveys);
            //add the action bar to the layout 
            ActionBar.SetCustomView(Resource.Layout.action_bar);
            ActionBar.SetDisplayShowCustomEnabled(true);

            //action bar nav
            surveysBtn = FindViewById<LinearLayout>(Resource.Id.SurveyLayout);
            surveysBtn.Click += surveyBtn_Click;
            inboxBtn = FindViewById<LinearLayout>(Resource.Id.InboxLayout);
            inboxBtn.Click += InboxBtn_Click;
            availabilityBtn = FindViewById<LinearLayout>(Resource.Id.availabilityLayout);
            availabilityBtn.Click += availabilityBtn_Click;
            dashboardBtn = FindViewById<LinearLayout>(Resource.Id.dashboardLayout);
            dashboardBtn.Click += dashboardBtn_Click;
            surveyListview = FindViewById<ListView>(Resource.Id.surveyListView);
            surveyListview.ItemClick += SurveyListview_ItemClick;


            WebClient client = new WebClient();
            System.Uri uri = new System.Uri("http://dribl.com/api/getAllMySurveys");
            NameValueCollection parameters = new NameValueCollection();


            parameters.Add("token", GlobalVariables.token);

            client.UploadValuesAsync(uri, parameters);
            client.UploadValuesCompleted += client_UploadValuesCompleted;
        }


        //listview row click 
        String survey_ID;
        private void SurveyListview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {



            Intent intent = new Intent(this, typeof(muscleCondition));
            intent.PutExtra("survey_id", "1");
            StartActivity(intent); 
        }

        void client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            string json = Encoding.UTF8.GetString(e.Result);
            List<Survey> survey = JsonConvert.DeserializeObject<List<Survey>>(json);


            //get the list view create a string to store and add to the list view based on the json return
            surveyListview = FindViewById<ListView>(Resource.Id.surveyListView);
            surveys = new List<string>();

            for (int c = 0; c < survey.Count; c++)
            {
                //if (survey[c].survey != null)
                //idtxtview.text = survey[c].id + etc
                surveys.Add(survey[c].id + "." + " " + "[" + survey[c].type + "]" + " " + "Date: " + survey[c].created_at);
            }


            ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, surveys);

            surveyListview.Adapter = adapter;


        }

        void surveyBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(Surveys));
            this.StartActivity(intent);
            this.Finish();
        }

        void dashboardBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(dashboard));
            this.StartActivity(intent);
            this.Finish();
        }

        void availabilityBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(Availability));
            this.StartActivity(intent);
            this.Finish();
        }

        void InboxBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(MsgInbox));
            this.StartActivity(intent);
            this.Finish();
        }


    }

    public class Survey
    {

        public int id { get; set; }
        public string type   { get; set; }
        public string created_at { get; set;}

    }

}
james.d_12
  • 57
  • 8

1 Answers1

0

James try this code

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Newtonsoft.Json;

namespace Dribl.Droid
{
    [Activity(Label = "Surveys", Theme = "@style/CustomActionBarTheme")]
    public class Surveys : Activity
    {
        LinearLayout surveysBtn;
        LinearLayout availabilityBtn;
        LinearLayout inboxBtn;
        LinearLayout dashboardBtn;

        //Button backBtn;

        private List<String> surveys;
        private ListView surveyListview;
        private List<Survey> survey; // notice the survey list here
        private ArrayAdapter<string> adapter; // notice adapter here

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Surveys);
            //add the action bar to the layout 
            ActionBar.SetCustomView(Resource.Layout.action_bar);
            ActionBar.SetDisplayShowCustomEnabled(true);

            //action bar nav
            surveysBtn = FindViewById<LinearLayout>(Resource.Id.SurveyLayout);
            surveysBtn.Click += surveyBtn_Click;
            inboxBtn = FindViewById<LinearLayout>(Resource.Id.InboxLayout);
            inboxBtn.Click += InboxBtn_Click;
            availabilityBtn = FindViewById<LinearLayout>(Resource.Id.availabilityLayout);
            availabilityBtn.Click += availabilityBtn_Click;
            dashboardBtn = FindViewById<LinearLayout>(Resource.Id.dashboardLayout);
            dashboardBtn.Click += dashboardBtn_Click;
            surveyListview = FindViewById<ListView>(Resource.Id.surveyListView);
            surveyListview.ItemClick += SurveyListview_ItemClick;


            WebClient client = new WebClient();
            System.Uri uri = new System.Uri("http://dribl.com/api/getAllMySurveys");
            NameValueCollection parameters = new NameValueCollection();


            parameters.Add("token", GlobalVariables.token);

            client.UploadValuesAsync(uri, parameters);
            client.UploadValuesCompleted += client_UploadValuesCompleted;
        }

        //listview row click // notice we get the selected survey string here using click event arguments 
        String survey_ID;
        private void SurveyListview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
             var selectedSurvey = survey.ElementAt(e.Position); // first approach or var selectedSurvey = survey[e.Position];
            //var selectedSurvey = adapter.GetItemAtPosition(e.Position); // second approach get selected survey string

            // do something with the selectedSurvey
            Intent intent = new Intent(this, typeof(muscleCondition));
            intent.PutExtra("survey_id", selectedSurvey.getId()); //pass survey id here
            StartActivity(intent); 
        }

        void client_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            string json = Encoding.UTF8.GetString(e.Result);
            survey = JsonConvert.DeserializeObject<List<Survey>>(json);

            //get the list view create a string to store and add to the list view based on the json return
            surveyListview = FindViewById<ListView>(Resource.Id.surveyListView);
            surveys = new List<string>();

            for (int c = 0; c < survey.Count; c++)
            {                   
                surveys.Add(survey[c].id + "." + " " + "[" + survey[c].type + "]" + " " + "Date: " + survey[c].created_at);
            }

            adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, surveys);

            surveyListview.Adapter = adapter;
        }

        void surveyBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(Surveys));
            this.StartActivity(intent);
            this.Finish();
        }

        void dashboardBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(dashboard));
            this.StartActivity(intent);
            this.Finish();
        }

        void availabilityBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(Availability));
            this.StartActivity(intent);
            this.Finish();
        }

        void InboxBtn_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(MsgInbox));
            this.StartActivity(intent);
            this.Finish();
        }
    }

    public class Survey
    {
        public int id { get; set; }
        public string type   { get; set; }
        public string created_at { get; set;}
    }
}
Rishabh Bhatia
  • 1,019
  • 6
  • 14
  • The first approach gives me this : /Users/James/Projects/Dribl/Droid/Surveys.cs(32,32): Error CS1061: 'List' does not contain a definition for 'get' and no extension method 'get' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Dribl.Droid) – james.d_12 May 19 '17 at 03:53
  • the second gives me this : – james.d_12 May 19 '17 at 03:54
  • /Users/James/Projects/Dribl/Droid/Surveys.cs(42,42): Error CS1061: 'ArrayAdapter' does not contain a definition for 'GetItemAtPosition' and no extension method 'GetItemAtPosition' accepting a first argument of type 'ArrayAdapter' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Dribl.Droid) – james.d_12 May 19 '17 at 03:54
  • @james.d_12 This will help http://stackoverflow.com/questions/15456845/getting-a-list-item-by-index. Change var selectedSurvey = survey.get(e.Position); to var selectedSurvey = survey[e.Position]; – Rishabh Bhatia May 19 '17 at 04:12
  • or use var selectedSurvey = survey.ElementAt(e.Position) requires using System.Linq; – Rishabh Bhatia May 19 '17 at 04:13
  • ok so that gives me everything as in date, id and type, how can I now just abstract just the id.? – james.d_12 May 19 '17 at 04:23
  • @james.d_12 Can you add the response to your comment. var surveyId = selectedSurvey.id doesn't do the trick? – Rishabh Bhatia May 19 '17 at 04:40
  • The response gives me all item in the class Survey. – james.d_12 May 19 '17 at 09:33
  • Which is what the list view displays. So sends to the next activity what ever was in the row I clicked. And selectedSurvey.id didnt work – james.d_12 May 19 '17 at 09:34
  • Anyone else able to help? – james.d_12 May 20 '17 at 00:40