gpt4 book ai didi

c# - 在 Listview 自定义适配器中编辑文本在滚动时丢失了它的位置? - c# - Xamarin.Android

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:00 27 4
gpt4 key购买 nike

当我们在第 1 行输入值时,当我们滚动到第 6 行时,第 1 行中输入的值会出现在第 6 行中。请参阅下面的代码和建议。

namespace Kites
{
public class Marks
{
// add any if you need more

public string StudentName { get; set; }
public string MarksScored { get; set; }
}

public class TEXTCHECK
{
public int POS { get; set; }
public string Value { get; set; }
}

public class MarksListViewAdapter : BaseAdapter<Marks>
{
private List<Marks> mstuduentmarks;
private List<TEXTCHECK> abc = new List<TEXTCHECK>();
private Context mcontext;

public MarksListViewAdapter (Context context, List<Marks> stud)
{
mstuduentmarks = stud;
mcontext = context;
}

public override int Count
{
get
{
return mstuduentmarks.Count;
// return mattendence.Count;

}
}
public override long GetItemId (int position)
{
return position;
}
public override Marks this[int position]
{
get
{
return mstuduentmarks [position];
// return mattendence [position];

}
}

class ViewHolder : Java.Lang.Object
{
public EditText comsevin;
public TextView namenmn;
}

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

ViewHolder holder;
View view = convertView;

if (view == null) // otherwise create a new one
{
view = LayoutInflater.From(mcontext).Inflate(Resource.Layout.listview_Marks, null, false);
holder = new ViewHolder();
holder.comsevin = view.FindViewById<EditText>(Resource.Id.editTextTeacherMarks);
holder.namenmn = view.FindViewById<TextView>(Resource.Id.textStudentNameTeacherMarks);
holder.namenmn.Tag = position;
view.Tag = holder;
}
else
{
holder = (ViewHolder)view.Tag;
}

holder.namenmn.Text = mstuduentmarks[position].StudentName;

int pos = (int)holder.namenmn.Tag;

holder.comsevin.TextChanged += (sender, e) =>
{
abc[pos].Value = holder.comsevin.Text;
};

//TextView txtStudent =
//txtStudent.Text = mstuduentmarks[position].StudentName;


//txtMarks.FocusChange += (object sender, View.FocusChangeEventArgs e) =>
//{
// //txtMarks.RequestFocusFromTouch ();

// mstuduentmarks[position].MarksScored = txtMarks.Text;
//};



holder.comsevin.BeforeTextChanged += (sender, e) =>
{
abc.Add(new TEXTCHECK { POS = position, Value = mstuduentmarks[position].MarksScored });
};
holder.comsevin.AfterTextChanged += (sender, e) =>
{
int a = abc[pos].POS;
mstuduentmarks[pos].MarksScored = abc[pos].Value;
};


//txtMarks.Tag = position;
//txtMarks.TextChanged += TxtMarks_TextChanged;
return view;
}

//void TxtMarks_TextChanged (object sender, Android.Text.TextChangedEventArgs e)
//{
// EditText txtMarks = (EditText)sender;
// //var position = (int)txtMarks.Tag;



//}
}
}

当我们在第 1 行输入值时,当我们滚动到第 6 行时,第 1 行中输入的值会出现在第 6 行中。请参阅下面的代码和建议。

最佳答案

根据经验,当在 listview/recyclerview 中遇到不反射(reflect)数据集的列表(例如遇到项目重复)时,这意味着您正在使用以前使用过的脏 View ,然后错误地重新绑定(bind), 或者只是在绑定(bind)期间使用了错误的位置

我知道你错在哪里了:

if (view == null) // otherwise create a new one
{
view = LayoutInflater.From(mcontext).Inflate(Resource.Layout.listview_Marks, null, false);
holder = new ViewHolder();
holder.comsevin = view.FindViewById<EditText>(Resource.Id.editTextTeacherMarks);
holder.namenmn = view.FindViewById<TextView>(Resource.Id.textStudentNameTeacherMarks);


holder.namenmn.Tag = position;//<------------here!!!
view.Tag = holder;
}

TLDR 不要以这种方式保存职位。

发生了什么:您的 View 实例正在被 listView 重用,这意味着有时(很多次)if (view == null) 将是 false 而这表示第 6 行(或任何其他将使用回收 View 的调用)的标签属性不会更新,并且您实际上使用的是脏值。

然后您尝试使用 Tag 属性作为位置,但如果 View 被回收,则忘记此标记已经很脏了

int pos = (int)holder.namenmn.Tag;

holder.comsevin.TextChanged += (sender, e) =>
{
abc[pos].Value = holder.comsevin.Text;
};

由于您有权访问此方法调用中的位置,因此您应该直接使用它

看看这个guide来自 Java 代码极客,即使它是在 Java 中,您也将能够看到旧 ViewHolder/ListView 模式的良好实现。


希望对你有帮助

关于c# - 在 Listview 自定义适配器中编辑文本在滚动时丢失了它的位置? - c# - Xamarin.Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40303551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com