Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c listview添加第二列行数据_C#_Listview_Xamarin - Fatal编程技术网

C# c listview添加第二列行数据

C# c listview添加第二列行数据,c#,listview,xamarin,C#,Listview,Xamarin,所以我有一个voteTotal变量,它将计算总共7个州的选举人票。然而,数字在第二列的不同行上,但我不确定如何添加第二列中的选举人票。第一栏是州名,第二栏是选举人票总数 我真的不知道如何调用这些行,所以我很难在每行的同一列2上添加正确的行2-8 int voteTotal = 0; int stateNumOne = 0; int stateNumTwo = 0; int stateNumThree = 0; int stateNumFour = 0; int stateNumFive = 0;

所以我有一个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;

我不确定这是否是你想要做的,但这篇文章给出的第一个答案听起来像是你应该做的

您应该使用数据绑定并使用它来获取数据。您不应直接接触单元格/项目


但话说回来,你的问题本身有点模糊。我希望这会有所帮助。

您是否希望获得以下截图那样的结果?

这里是布局图

  <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>
下面是MyViewModel的代码


Yopu应该为您的问题添加一个标记,指示您正在使用的平台,因为每个平台上都有一个ListView,我们真的不喜欢猜测。您的意思是添加visual studio?不。visual studio是一个开发环境,而不是一个平台。从您发布的代码来看,您使用的平台可能是Xamarin。我认为默认情况下是Xamarin,因为我刚刚下载了visual studio 2017,就是这样。谷歌称,VisualStudio2017使您能够使用Xamarin和C或Java/C++构建原生Android应用程序,并使用Apache Cordova 6.3.1和JavaScript以及TypeScription构建混合Android应用程序。这没有什么帮助,但感谢您的尝试。下面是我在listview中的行/列示例。我想做的是在我的列表视图中添加选举人票,从第二行和第二列开始,然后向上。州名,选举人票俄勒冈州7华盛顿州8加利福尼亚州55这不是数据绑定固定的吗?然后你要做的就是把变量编辑成一个变量,因为单元格值绑定到它,它会随着变量的变化而变化。我遇到的问题是,我知道如何检索第一行,但我认为我的代码检索第二行是不对的。当我看你给我看的示例时,我感到困惑,因为我无法直观地看到代码将如何创建listview。在我的listview中,每行有2列,我只需要每行的第2列并添加它们。在这些示例中,我不确定它们是否都在一行多列中,或者它们是否都在多行中(如果有意义的话)。您是否尝试过简单地获取项目的计数,然后在listview中循环,根据循环索引添加从第二列中获得的所有值?看起来这比一个一个的做要简单得多哦我不知道怎么做
  BindingContext = new 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()});



    }
}
     public class MyModel
{
    public string State { get; set; }
    public string Votes { get; set; }
}

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());
        }
    }