c# - 从从 api 填充的 ListView 行单击中提取数据

标签 c# android api listview xamarin

好的,我有一个从 API 填充的 ListView 。 ListView 只是显示一个调查列表,供用户单击并完成。 ListView 显示调查的 ID、类型和创建时间(日期)。

我能够获取要传递到下一个屏幕的 id、type 和 created_at 文本,但我只想通过 ID 发送,但无法计算出来?

我如何只从 ListView 行中提取 id,然后使用 intent 将其发送到下一个屏幕?

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)
        {

            //var selectedSurvey = surveys.ElementAt(e.Position);
            //survey_ID = selectedSurvey;
            //var surveyId = Survey selectedSurvey.id;

            var survey = surveys.ElementAt(e.Position);
            survey_ID = survey;


            Intent intent = new Intent(this, typeof(muscleCondition));
            intent.PutExtra("survey_id", survey_ID);
            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; }

    }

}

最佳答案

       var survey = surveys.ElementAt(e.Position);
       var survey_ID = survey.Split('.')[0];

关于c# - 从从 api 填充的 ListView 行单击中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44082865/

相关文章:

c# - RavenDb NLog 配置冲突

c# - .NET Web 应用程序的尖端技术?

javascript - Facebook SDK 权限

node.js - 调用 ExpressJS REST 路由时出现奇怪的行为

c# - 在对我的 API 进行版本控制时,如果我使用相同的 DTO,我该如何维护 swagger 文档?

c# - 如何在使用 roslyn 编译期间向程序集发出资源?

c# - 无法检测到正确的字符编码

android - Firemonkey 为 Android 启用 GPS 服务

Android - 焦点导航后退按钮

android - AppWidget 中的 GridView View 回收