无法从xamarin中的自定义列表视图适配器获取所选的微调器值。 C#

时间:2016-05-08 11:34:35

标签: c# android xamarin xamarin.android

Xamarin帮助。

无法在自定义列表视图适配器中获取微调器的选定值。

MyListViewAdapter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace Kites
{
public class AttendenceViewAdapter : BaseAdapter<string>
{
    private List<string> mstudents;

    private Context mcontext;



    public AttendenceViewAdapter(Context context, List<string> stud)
    {
        mstudents = stud;
        mcontext = context;


    }
    public override int Count 
    {
        get 
        {
            return mstudents.Count;


        }
    }
    public override long GetItemId (int position)
    {
        return position;
    }
    public override string this[int position] 
    {
        get 
        {
            return mstudents [position].ToString();


        }
    }



    public override View GetView (int position, View convertView,   ViewGroup parent)
    {
        View view = convertView;
        if (view == null) // otherwise create a new one
            view = LayoutInflater.From(mcontext).Inflate(Resource.Layout.listview_attendence , null, false);
        // set view properties to reflect data for the given row
        TextView txtStudent = view.FindViewById<TextView>(Resource.Id.textStudentNameTeacherAttendence);
        txtStudent.Text = mstudents[position];
        Spinner spinner = view.FindViewById<Spinner> (Resource.Id.spinnerTeacherAttendence);



        var adapter = ArrayAdapter.CreateFromResource (mcontext, Resource.Array.attendence_array, Android.Resource.Layout.SimpleDropDownItem1Line);

        adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);

        spinner.Focusable = false;
        spinner.FocusableInTouchMode = false;
        spinner.Clickable = true;

        spinner.ItemSelected += Spinner_ItemSelected;

        spinner.Adapter = adapter;
        var abcde = spinner.SelectedItemPosition;
        spinner.SetSelection (abcde);




        // return the view, populated with data, for display
        return view;
    }

    void Spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;
        var abc = spinner.SelectedItemPosition;

        spinner.RequestFocusFromTouch ();
        spinner.SetSelection (abc);
    }



}
}

主要活动。

ListView lsStu = FindViewById<ListView> (Resource.Id.listStudentAttendence);
        AttendenceViewAdapter adapter = new AttendenceViewAdapter (this, mstudents);
lsStu.Adapter = adapter;


for(int i = 0; i < abc; i++)
            {

            View view = adapter.GetView(i, null, null);
            TextView txtStudentName = view.FindViewById<TextView> (Resource.Id.textStudentNameTeacherAttendence);
            string txtSN = txtStudentName.Text;
            Spinner spinner = view.FindViewById<Spinner> (Resource.Id.spinnerTeacherAttendence);

            var txtS = spinner.SelectedItem;
// I Cant Get The Selected Value Here. , I Am Getting the default Value not the selected value .do i do that

            string studentattendence = "insert into student_attendence values ('" + txtClassID.Text + "','" + txtClassName.Text + "','" + txtDate.Text + "','" + txtSN + "','" + txtS + "');";

                SqlCommand abcdedh = new SqlCommand (studentattendence, con);
                abcdedh.ExecuteNonQuery ();

                Toast.MakeText (this, "Attendence Saved", ToastLength.Short).Show();


            }

我在位置0获得默认值,但不是我选择的值。 当我在mylistviewadapter中放置一个断点时,每当我选择一个新值时我都会得到所选的值,但是我没有在主要活动中得到它,请帮助我。

1 个答案:

答案 0 :(得分:0)

问题是:您致电adapter.GetView(i, null, null);因此第二个参数convertViewnull,这意味着您将为新视图充气。

此方法失败,因为GetView不是要从ListView或使用适配器的视图外部调用。这将失败的第二个原因是称为视图重用的机制。如果你的列表中有100个项目,那么创建的视图就会少得多(只能同时显示儿子屏幕,再加上一些以便平滑滚动)。

您应该使用的方法是:

创建一个类Attendence ,其中包含Name和您需要的所有其他值。

public class Attendence
{
    public string ClassId { get; set; }
    public string ClassName { get; set; }
    public string StudentName { get; set; }
    public string Attendence { get; set; }
    //TODO: maybe some more
}

继承自 BaseAdapter<Attendence>

如果列表项中的值发生变化,请将其写回当前学生(例如,在Spinner_ItemSelected中)。

public class AttendenceViewAdapter : BaseAdapter<Attendence>
{
    private List<Attendence> mstudents;
    private Context mcontext;

    public AttendenceViewAdapter(Context context, List<Attendence> stud)
    {
        mstudents = stud;
        mcontext = context;
    }

    // ...

    public override View GetView (int position, View convertView,   ViewGroup parent)
    {
        View view = convertView;
        Spinner spinner;
        if (view == null){
            view = LayoutInflater.From(mcontext).Inflate(Resource.Layout.listview_attendence , null, false);
            spinner = view.FindViewById<Spinner> (Resource.Id.spinnerTeacherAttendence);
            var adapter = ArrayAdapter.CreateFromResource (mcontext, Resource.Array.attendence_array, Android.Resource.Layout.SimpleDropDownItem1Line);

            adapter.SetDropDownViewResource (Android.Resource.Layout.SimpleSpinnerDropDownItem);

            spinner.Focusable = false;
            spinner.FocusableInTouchMode = false;
            spinner.Clickable = true;

            spinner.ItemSelected += Spinner_ItemSelected;
            spinner.Adapter = adapter;
        }
        else {
            spinner = view.FindViewById<Spinner> (Resource.Id.spinnerTeacherAttendence);
        }

        // set view properties to reflect data for the given row
        TextView txtStudent = view.FindViewById<TextView>(Resource.Id.textStudentNameTeacherAttendence);
        txtStudent.Text = mstudents[position].StudentName;

        // TODO: use mstudents[position].Attendence here
        var abcde = spinner.SelectedItemPosition;
        spinner.SetSelection(abcde);
        spinner.Tag = position; // use this to know which student has been edited

        // return the view, populated with data, for display
        return view;
    }

    void Spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
    {
        Spinner spinner = (Spinner)sender;
        var abc = spinner.SelectedItemPosition;
        var position = (int)spinner.Tag;

        spinner.RequestFocusFromTouch ();
        spinner.SetSelection (abc);

        mstudents[position].Attendence = //TODO: selected value of the spinner;
    }
}

在您的MainActivity 中,您可以遍历学生列表,而不是列表中的视图。

// given: students is your list of Attendence   
foreach(Attendence attendence in students)
{
    string studentattendence = "insert into student_attendence values ('" + attendence.ClassId + "','" + attendence.ClassName + "','" + txtDate.Text + "','" + txtSN + "','" + attendence.Attendence + "');";

    SqlCommand abcdedh = new SqlCommand (studentattendence, con);
    abcdedh.ExecuteNonQuery ();

    Toast.MakeText (this, "Attendence Saved", ToastLength.Short).Show();
}