我正在使用Xamarin Android写作,而我正试图处理ListView中的滚动问题。当我向下滚动时,再向上一次EditText失去先前输入的值。我用ViewHolder模式编写了自定义适配器。不幸的是,我的适配器无法正常工作,它将输入的数据混合到随机(?)行。
这是我的适配器。有人能告诉我代码中的问题在哪里吗?
public class Adapter: BaseAdapter<Product>
{
private List<Product> MyItems;
private Context MyContext;
public string[] Current;
Dictionary<int, string> myDictionary = new Dictionary<int, string>();
public Adapter(Context context, List<Product> items)
{
MyContext = context;
MyItems = items;
Current = new string[items.Count];
for (int i = 0; i < items.Count; i++)
{
myDictionary.Add(i, "0");
}
}
public override Product this[int position]
{
get { return MyItems[position]; }
}
public override int Count
{
get { return MyItems.Count; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
ViewHolder holder = null;
int nPostion = position;
if (row == null)
{
row = LayoutInflater.From(MyContext).Inflate(Resource.Layout.productRowOrder, parent, false);
holder = new ViewHolder();
holder.tv1 =row.FindViewById<TextView>(Resource.Id.productOrderId);
holder.tv1.Text = "" + MyItems[position].id;
holder.tv2 =row.FindViewById<TextView>(Resource.Id.productOrderName);
holder.tv2.Text = MyItems[position].name;
holder.tv3 =row.FindViewById<TextView>(Resource.Id.productOrderStock);
holder.tv3.Text = "" + MyItems[position].quantity;
holder.tv4 =row.FindViewById<TextView>(Resource.Id.productOrderUnit);
holder.tv4.Text = "" + MyItems[position].unit;
holder.tv5 =row.FindViewById<TextView>(Resource.Id.productOrderPriceNet);
holder.tv5.Text = "" + MyItems[position].priceNet;
holder.tv6 = row.FindViewById<TextView>(Resource.Id.productOrderPriceGross);
holder.tv6.Text = "" + MyItems[position].priceGross;
holder.et1 = row.FindViewById<EditText>(Resource.Id.productOrderQuantity);
row.Tag = holder;
}
else
{
holder = (ViewHolder)row.Tag;
}
holder.et1.AfterTextChanged += (sender, args) =>
{
Current[holder.pos] = args.Editable.ToString();
myDictionary[nPostion] = args.Editable.ToString().Trim();
};
holder.pos = position;
holder.et1.Text = myDictionary[position];
return row;
}
private class ViewHolder : Java.Lang.Object
{
public TextView tv1;
public TextView tv2;
public TextView tv3;
public TextView tv4;
public TextView tv5;
public TextView tv6;
public EditText et1;
public int pos;
}
}
}
答案 0 :(得分:0)
从代码中,此行holder.et1.Text = myDictionary[position];
是滚动列表视图时设置edittext的值的位置。因此,数据源myDictionary
很可能处于无序状态。这一行myDictionary[nPostion] = args.Editable.ToString().Trim();
是我们将特定值放入edittext的地方。
我的解决方案是您可以尝试使用此行myDictionary[nPostion] = holder.et1.getText().ToString().Trim();
而不是myDictionary[nPostion] = args.Editable.ToString().Trim();
。(之前我没有使用过Xamarin,因此我使用了getText()〜)。我希望这个改变对修复你的bug很有用。
答案 1 :(得分:0)
不使用“AfterTextChanged”处理程序,而是处理“FocusChange”事件...... 它将解决您在Xamarin中的问题...
祝你好运。
---代码示例---
public override View GetView(int position, View convertView, ViewGroup parent)
{
var row = convertView;
if (row == null)
{
row = LayoutInflater.From(mContext).Inflate(Resource.Layout.StudentScore_item, null, false);
}
var lblScoreId = row.FindViewById<TextView>(Resource.Id.lblScoreID);
lblScoreId.Text = mItems[position].ID.ToString();
var tbxScore_Final = row.FindViewById<EditText>(Resource.Id.tbxScore_Final);
tbxScore_Final.Typeface = fontType;
tbxScore_Final.Text =
(mItems[position].FinalScore.HasValue ? mItems[position].FinalScore.ToString() : "");
tbxScore_Final.Tag = mItems[position].ID;
tbxScore_Final.FocusChange += (sender, e) =>
{
EditText tbx = sender as EditText;
decimal score;
bool isValidScore = decimal.TryParse(tbx.Text, out score);
if (isValidScore)
{
int inx = FindScoreIndex((int)tbx.Tag);
Log.WriteLine(
LogPriority.Info,
"Infooo",
string.Format(
"INDEX = {0} | ID = {1} | DATA = {2} | Validated = {3} | ==> {4}",
inx,
tbx.Tag.ToString(),
tbx.Text,
score.ToString(),
mItems[inx].ID
)
);
mItems[inx].FinalScore = score;
}
};
return row;
}