过滤后单击listview时,我收到了错误的项目

时间:2017-10-15 12:37:32

标签: c# xamarin

我使用了来自JoeRock频道的示例。当我在过滤listview后使用项目点击事件时存在问题

这是我的班级:

class Friend
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }
    public string Gender { get; set; }
}

这是我的朋友适配器

class FriendsAdapter : BaseAdapter<Friend>
{
    private Context mContext;
    private int mRowLayout;
    private List<Friend> mFriends;


    public FriendsAdapter(Context context, int rowLayout, List<Friend> friends)
    {
        mContext = context;
        mRowLayout = rowLayout;
        mFriends = friends;

    }

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

    public override Friend this[int position]
    {
        get { return mFriends[position]; }
    }

    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;

        if (row == null)
        {
            row = LayoutInflater.From(mContext).Inflate(mRowLayout, parent, false);
        }


        TextView firstName = row.FindViewById<TextView>(Resource.Id.txtFirstName);
        firstName.Text = mFriends[position].FirstName;

        TextView lastName = row.FindViewById<TextView>(Resource.Id.txtLastName);
        lastName.Text = mFriends[position].LastName;

        TextView age = row.FindViewById<TextView>(Resource.Id.txtAge);
        age.Text = mFriends[position].Age;

        TextView gender = row.FindViewById<TextView>(Resource.Id.txtGender);
        gender.Text = mFriends[position].Gender;



        return row;
    }

这是我的活动

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

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        mListView = FindViewById<ListView>(Resource.Id.listView);
        mSearch = FindViewById<EditText>(Resource.Id.etSearch);
        mContainer = FindViewById<LinearLayout>(Resource.Id.llContainer);

        mTxtHeaderFirstName = FindViewById<TextView>(Resource.Id.txtHeaderFirstName);
        mTxtHeaderLastName = FindViewById<TextView>(Resource.Id.txtHeaderLastName);
        mTxtHeaderAge = FindViewById<TextView>(Resource.Id.txtHeaderAge);
        mTxtHeaderGender = FindViewById<TextView>(Resource.Id.txtHeaderGender);


        mSearch.Alpha = 0;
        mContainer.BringToFront();

        mSearch.TextChanged += mSearch_TextChanged;

        mFriends = new List<Friend>();
        mFriends.Add(new Friend { FirstName = "Bob", LastName = "Smith", Age = "33", Gender = "Male" });
        mFriends.Add(new Friend { FirstName = "Tom", LastName = "Smith", Age = "45", Gender = "Male" });
        mFriends.Add(new Friend { FirstName = "Julie", LastName = "Smith", Age = "2020", Gender = "Unknown" });
        mFriends.Add(new Friend { FirstName = "Molly", LastName = "Smith", Age = "21", Gender = "Female" });
        mFriends.Add(new Friend { FirstName = "Joe", LastName = "Lopez", Age = "22", Gender = "Male" });
        mFriends.Add(new Friend { FirstName = "Ruth", LastName = "White", Age = "81", Gender = "Female" });
        mFriends.Add(new Friend { FirstName = "Sally", LastName = "Johnson", Age = "54", Gender = "Female" });

        mAdapter = new FriendsAdapter(this, Resource.Layout.row_friend, mFriends);
        mListView.Adapter = mAdapter;

         mListView.ItemClick += Mlistview_ItemClick;//The Problem is Here:

    }
    private void Mlistview_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
       string test = mFriends[e.Position].FirstName;
        Toast.MakeText(this, test, ToastLength.Short).Show();


    }


    void mSearch_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
    {
        List<Friend> searchedFriends = (from friend in mFriends
                                        where friend.FirstName.Contains(mSearch.Text, StringComparison.OrdinalIgnoreCase) || friend.LastName.Contains(mSearch.Text, StringComparison.OrdinalIgnoreCase)
                                        || friend.Age.Contains(mSearch.Text, StringComparison.OrdinalIgnoreCase) || friend.Gender.Contains(mSearch.Text, StringComparison.OrdinalIgnoreCase)
                                        select friend).ToList<Friend>();

        //Refreshes the listview
        mAdapter = new FriendsAdapter(this, Resource.Layout.row_friend, searchedFriends);
        mListView.Adapter = mAdapter;
    }

问题在于,当我从listBox过滤我的列表视图并且我选择了一个项目时,它并没有向我显示正确的名称,而是在我的特定列表位置中存在于mitems中的名称。

我不知道如何解决这个问题。即使对于java,任何答案都被接受。我只需要一个解决方案来解决这个问题。看下面的图片

此处代码正常 Here Code works right

这里我的名字错误 Here i'm taking wrong Name

我读到我必须创建一个RecyclerView.Adapter。是吗?怎么办呢?

1 个答案:

答案 0 :(得分:0)

替换

string test = mFriends[e.Position].FirstName;

string test = ((Friend)mAdapter.GetItem(e.Position)).FirstName;

原因是在文本更改事件中,您正在创建具有新列表的新适配器。此列表在mAdapter中可用。