我正在研究付款功能。我对每件商品GridView
CheckBox
。我需要将选定的网格项目带到下一个片段&做一些计算。
我尝试过如下
btnPtoPay.Click += delegate
{
for (int x = 0; x < tableGrid.ChildCount; x++)
{
var checkbox = (CheckBox)tableGrid.GetChildAt(x).FindViewById(Resource.Id.txtCellFive);
var tvSelectedAmount = (TextView)tableGrid.GetChildAt(x).FindViewById(Resource.Id.txtCelltwo);
var tvAmountPaid = (TextView)tableGrid.GetChildAt(x).FindViewById(Resource.Id.txtCelltwo);
decimal totalM = 0;
if (checkbox.Checked)
{
totalM = totalM+ Convert.ToDecimal(tvSelectedAmount.Text);
//Here how to get selected item, with all fields
}
};
var fragmentTx = Activity.SupportFragmentManager.BeginTransaction();
var feePay = new FeePaymentFragment();
fragmentTx.Replace(Resource.Id.crealtabcontent, feePay, "feedPayFragmentTag").AddToBackStack("feePayfrg");
fragmentTx.Commit();
};
如何获取所选项目?
请参阅屏幕截图以获取更多信息
答案 0 :(得分:1)
您正在寻找的是基本的OOP实践。为每个项创建一个容器对象,并通过构造函数将所有选中的项传递给新的片段。这是一个非常基本的示例,基于您在问题中提供的代码:
class LineItem
{
//you should probably have an id in here to differentiate objects easier
public double TotalFee { get; }
public double AmountPaid { get; }
public double SelectedAmount { get; }
public LineItem(double totalFee, double amountPaid, double selectedAmount)
{
TotalFee = totalFee;
AmountPaid = amountPaid;
SelectedAmount = selectedAmount;
}
}
上面的类只是作为一个容器,所以你可以传递数据并以高效和冗长的方式检索它。
class FeePaymentFragment : Fragment
{
private List<LineItem> _selectedList;
public FeePaymentFragment(List<LineItem> selectedList)
{
_selectedList = selectedList;
}
//do the rest of your fragment stuff, referencing _selectedList when you
//need to access the selected objects
}
上面的类是你的片段类;重要的注意事项是构造函数,它接受LineItem
个对象的列表。这样您就可以在需要时引用它们。
btnPtoPay.Click += delegate
{
List<LineItem> selectedList = new List<LineItem>();
for (int x = 0; x < tableGrid.ChildCount; x++)
{
var checkbox = (CheckBox)tableGrid.GetChildAt(x).FindViewById(Resource.Id.txtCellFive);
var tvSelectedAmount = (TextView)tableGrid.GetChildAt(x).FindViewById(Resource.Id.txtCelltwo);
var tvAmountPaid = (TextView)tableGrid.GetChildAt(x).FindViewById(Resource.Id.txtCelltwo);
decimal totalM = 0;
if (checkbox.Checked)
{
totalM = totalM+ Convert.ToDecimal(tvSelectedAmount.Text);
//Here how to get selected item, with all fields
double selectedAmount = double.Parse(tvSelectedAmount.Text);
double amountPaid = double.Parse(tvAmountPaid);
selectedList.Add(new LineItem(totalM, amountPaid, selectedAmount));
}
};
var fragmentTx = Activity.SupportFragmentManager.BeginTransaction();
var feePay = new FeePaymentFragment(selectedList);
fragmentTx.Replace(Resource.Id.crealtabcontent, feePay, "feedPayFragmentTag").AddToBackStack("feePayfrg");
fragmentTx.Commit();
};
沿着这些方向的某些内容将帮助您开始将选择从一个地方移动到另一个地方。从这里,您可以根据需要在较小的网格中重新实例化已检查的项目,或者进行计算或任何您想要的任何内容。
您可能希望更好地进行适合您应用的更改,我只需要处理少量信息。