[英]Is there a way to remove System.Collection.Generic message from screen instead of string when display ListView data from db on c# Xamarin
I want when the user clicks on my first listView to show a second listview with different data from database, but I see this Message: "System.Collections.Generic.List'1[System..."
我希望当用户单击我的第一个 listView 以显示具有来自数据库的不同数据的第二个列表视图时,但我看到此消息:
"System.Collections.Generic.List'1[System..."
When I use debugger everything is fine.. I see the data from the date base..当我使用调试器时一切都很好..我看到了来自日期库的数据..
My second listview look like this:我的第二个列表视图如下所示:
<StackLayout>
<ListView x:Name="MeteoView"
ItemsSource="{Binding Forecast}"
HeightRequest="100"
IsVisible="false">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell
Text="{Binding DisplayRun}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
My c# code to connect with db, execute the procedure and display the data look like this:我的 C# 代码与 db 连接,执行程序并显示数据如下所示:
public class ForecastData
{
public string DisplayRun { get; set; }
}
ObservableCollection<ForecastData> MeteoData = new ObservableCollection<ForecastData>();
public ObservableCollection<ForecastData> Forecast { get { return MeteoData; } }
void listView_ItemSelected(System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
{
string str = ((Contacts)listView.SelectedItem).Place;
string substr = str.Substring(str.Length - 5);
searchBar.Placeholder = str;
server = "ip-to-db";
database = "db-name";
uid = "user";
password = "pass";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
List<string> Run = new List<string>();
try
{
connection.Open();
var cmd = new MySqlCommand();
cmd.Connection = connection;
MySqlCommand command = new MySqlCommand($"CALL `get_all_data`({substr})", connection);
MeteoView.IsVisible = true;
MeteoView.ItemsSource = MeteoData;
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Run.Add(reader[0].ToString());
MeteoData.Add(new ForecastData { DisplayRun = $"{Run}" });
}
}
connection.Close();
}
catch (Exception ex)
{
LabelSQL.Text = (ex.ToString());
}
}
So on my display emulator I see this one in the listView:所以在我的显示模拟器上,我在 listView 中看到了这个:
this is essentially the same problem you asked about earlier today.这与您今天早些时候问过的问题基本相同。 By default
ToString
will return the name of the class.默认情况下
ToString
将返回类的名称。 You are setting DisplayRun
to the contents of a List<string>
您正在将
DisplayRun
设置为List<string>
List<string> Run = new List<string>();
Run.Add(reader[0].ToString());
MeteoData.Add(new ForecastData { DisplayRun = $"{Run}" });
which is basically the same as这基本上与
DisplayRun = Run.ToString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.