因此,我有一个voteTotal变量,该变量将计算总共7个州的选举人票数。但是,数字在第二列的不同行上,但是我不确定如何添加第二列的选举人票。第一栏是州名,第二栏是选举人总数。
我真的不知道如何调用行,因此我很难在每行的同一列2上添加正确的行2-8。
int voteTotal = 0;
int stateNumOne = 0;
int stateNumTwo = 0;
int stateNumThree = 0;
int stateNumFour = 0;
int stateNumFive = 0;
int stateNumSix = 0;
int stateNumSeven = 0;
if (votesListView.Items.Count < 1)
{
MessageBox.Show("List View can not be empty.");
}
if (votesListView.Items[0].SubItems[1] != null)
{
stateNumOne = int.Parse(votesListView.Items[0].SubItems[1].Text);
MessageBox.Show("1");
}
if (votesListView.Items[1].SubItems[1] != null)
{
stateNumTwo = int.Parse(votesListView.Items[1].SubItems[1].Text);
MessageBox.Show("2");
}
else if (votesListView.Items[2].SubItems[1] != null)
{
stateNumThree = int.Parse(votesListView.Items[2].SubItems[1].Text);
}
/*
stateNumSix = int.Parse(votesListView.Items[6].SubItems[
stateNumFour = int.Parse(votesListView.Items[4].SubItems[1].Text);
stateNumFive = int.Parse(votesListVie1].Text);
stateNumSeven = int.Parse(votesListView.Items[7].SubItems[1].Text);
*/
voteTotal = stateNumOne + stateNumTwo + stateNumThree + stateNumFour + stateNumFive + stateNumSix + stateNumSeven;
totalLabel.Text = voteTotal.ToString();
totalVotesLabel.Visible = true;
totalLabel.Visible = true;
答案 0 :(得分:1)
我不确定这是否是您要尝试的方法,但是此帖子Xamarin.Forms: Get all cells/items of a listview的第一个答案听起来像您应该做的事情。
您应该使用数据绑定并使用它来获取数据。您不应该直接触摸单元格/项目。
但是再说一遍,您的问题本身有点含糊。希望对您有所帮助。
答案 1 :(得分:0)
这里是布局。
<ListView x:Name="listView" ItemsSource="{Binding MyModels} ">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="{Binding State}"
/>
<Label Grid.Row="0" Grid.Column="1" Text="{Binding Votes}"
/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是背景代码。
BindingContext = new MyViewModel();
这里是MyViewModel
class MyViewModel
{
public ObservableCollection<MyModel> MyModels { get; set; }
public MyViewModel()
{
MyModels = new ObservableCollection<MyModel>();
MyModels.Add(new MyModel() { State = "State Name", Votes = "Electoral Votes"
});
MyModels.Add(new MyModel() { State = "Oregon ", Votes = "7" });
MyModels.Add(new MyModel() { State = "Washington", Votes = "8" });
MyModels.Add(new MyModel() { State = "California", Votes = "55" });
MyModels.Add(new MyModel() { State = "lost1", Votes = "22" });
MyModels.Add(new MyModel() { State = "lost2", Votes = "24" });
MyModels.Add(new MyModel() { State = "lost3", Votes = "25" });
MyModels.Add(new MyModel() { State = "lost4", Votes = "26" });
MyModels.Add(new MyModel() { State = "lost5", Votes = "27" });
int sumVotes = 0;
for (int i=1;i<MyModels.Count;i++)
{
sumVotes+= Int32.Parse(MyModels[i].Votes);
}
MyModels.Add(new MyModel() { State = " ", Votes =
"sum"+sumVotes.ToString()});
}
}
此处是MyModel
的ID码
public class MyModel
{
public string State { get; set; }
public string Votes { get; set; }
}
答案 2 :(得分:0)
int voteTotal = 0;
int secondcolumn = 1;
enter code here
if (votesListView.Items.Count < 1)
MessageBox.Show("List View can not be empty.");
else
{
for(int spot = 0; spot < votesListView.Items.Count; spot++)
{
voteTotal += int.Parse(votesListView.Items[spot].SubItems[secondcolumn].Text);
//This will pop up a MessageBox at each iteration of this for loop.
//feel free to comment out the MessageBox if it doesn't work.
//you can also print out the voteTotal after the for loop.
MessageBox.Show(spot.ToString());
}
}