如何从列表中的Parse.com获取数据并将其应用于listview?

时间:2015-03-17 12:17:13

标签: c# parse-platform xamarin

我对Parse.com很新,似乎无法获取数据。 我尝试了一个异步方法,它做了一个foreach,但似乎从来没有工作,所以我尝试了ParseQuery,但这也没有用。 例如:

ParseQuery<ParseObject> query = ParseObject.GetQuery("Horse");
string name = query.Get<string>("Name");
Debug.WriteLine("Horse: " + name);

理想情况下,我希望从模型Horse获取所有记录,然后循环遍历它以填充列表视图。我现在有一个硬编码的样本,但由于我甚至没有从列表中的表中获取所有记录,我不知道如何继续。

var listView = new ListView
        {
            RowHeight = 40
        };
        listView.ItemsSource = new string[]
            {
            //This should be filled up with the field 'Name' from the model 'Horse' dynamically..
            "Buy pears",
            "Buy oranges",
            "Buy mangos",
            "Buy apples",
            "Buy bananas"
            };
        Content = new StackLayout
        {
            VerticalOptions = LayoutOptions.FillAndExpand,
            Children = { listView }
        };

请问有人在这帮助我吗?我遵循https://parse.com/docs/dotnet_guide#objects-retrieving的指南,但这些选项似乎不起作用。

更新 到目前为止,由于har07的答案,我可以获取所有数据。 现在,当我想要填充ListView时出现错误:

public async void getHorsesFromDb()
    {
        ParseQuery<ParseObject> query = ParseObject.GetQuery("Horse");
        IEnumerable<ParseObject> horses = await query.FindAsync();

        var listView = new ListView();
        List<string> horseList = null;

        foreach (ParseObject horse in horses)
        {
            string name = horse.Get<string>("Name");
            horseList.Add(name);
            Debug.WriteLine("Horse: " + name);
        }

        listView.ItemsSource = horseList;

        // Using ItemTapped
        listView.ItemTapped += async (sender, e) => {
            await DisplayAlert("Tapped", e.Item + " row was tapped", "OK");
            Debug.WriteLine("Tapped: " + e.Item);
            ((ListView)sender).SelectedItem = null; // de-select the row
        };
    }

我的屏幕出错:

The current stack frame was not found in a loaded module. Source cannot be shown for this location.
>   0x21 in System.Diagnostics.Debugger.Mono_UnhandledException_internal    C#

1 个答案:

答案 0 :(得分:5)

您所关注的链接显示了如何通过将特定ParseObject(该示例中的值为objectId)传递给查询来获取单行数据(xWMyZ4YEGZ)。

另一方面,要从表中获取多行,请遵循相同的Queries部分。获取所有行甚至应该更简单(到目前为止不需要编写任何LINQ):

ParseQuery<ParseObject> query = ParseObject.GetQuery("Horse");
IEnumerable<ParseObject> horses = await query.FindAsync();
foreach (ParseObject horse in horses)
{
    string name = horse.Get<string>("Name");
    Debug.WriteLine("Horse: " + name);
}