我有一个listview,其数据来自sqlite数据库。当用户选择上一页的数据来自JSON的列表时,将创建数据库。 代码:
ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
if (connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
{
loading.IsIndeterminate = true;
try
{
string urlPath = "https://.../tryout_perid";
var httpClient = new HttpClient(new HttpClientHandler());
var values = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("tid", ((App)(App.Current)).ID)
};
var response = await httpClient.PostAsync(urlPath, new FormUrlEncodedContent(values));
response.EnsureSuccessStatusCode();
if (!response.IsSuccessStatusCode)
{
loading.IsIndeterminate = false;
RequestException();
}
string jsonText = await response.Content.ReadAsStringAsync();
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonArray jsonData = jsonObject["data"].GetArray();
foreach (JsonValue groupValue1 in jsonData)
{
JsonObject groupObject2 = groupValue1.GetObject();
string id = groupObject2["id"].GetString();
string title = groupObject2["judul"].GetString();
QuizHome quiz = new QuizHome();
quiz.ID = id;
((App)(App.Current)).ID = quiz.ID.ToString();
quiz.Title = title;
quizDataSource.Add(quiz);
if (quizDataSource.Count > 0)
{
string InsertQuiz = @"INSERT INTO DBName (ID,Judul) SELECT '" + id.ToString() + "','" + title.ToString() + "','" + "' WHERE not exists " +
"(select ID and Judul FROM DBName WHERE ID='" + id.ToString() + "' and Judul='" + title.ToString() + "')";
var quizName = objConn.Prepare(InsertQuiz);
quizName.Step();
}
JsonArray jsonDataSoal = groupObject2["list_soal"].GetArray();
foreach (JsonValue groupValueSoal in jsonDataSoal)
{
JsonObject groupObjectSoal = groupValueSoal.GetObject();
string qid = groupObjectSoal["qid"].GetString();
string pertanyaan = groupObjectSoal["question"].GetString();
QuizQuestion question = new QuizQuestion();
question.QID = qid;
question.Pertanyaan = pertanyaan;
questionDataSource.Add(question);
if (questionDataSource.Count > 0)
{
string InsertQuestion = @"INSERT INTO DBQuestion (QID,Pertanyaan) SELECT '" + qid.ToString() + "','" + pertanyaan.ToString() + "' WHERE not exists " +
"(select QID and Pertanyaan FROM DBQuestion WHERE OID='" + qid.ToString() + "' and Pertanyaan='" + pertanyaan.ToString() + "')";
var quizQuestion = objConn.Prepare(InsertQuestion);
quizQuestion.Step();
}
JsonArray jsonDataOption = groupObjectSoal["jawaban"].GetArray();
foreach (JsonValue groupValueOption in jsonDataOption)
{
JsonObject groupObjectOption = groupValueOption.GetObject();
string oid = groupObjectOption["oid"].GetString();
string option = groupObjectOption["q_option"].GetString();
string score = groupObjectOption["score"].GetString();
QuizOption pilihan = new QuizOption();
pilihan.OID = oid;
pilihan.Option = option;
optionDataSource.Add(pilihan);
if (optionDataSource.Count > 0)
{
string InsertOption = @"INSERT INTO DBOption (OID,Option) SELECT '" + oid.ToString() + "','" + option.ToString() + "','" + "' WHERE not exists " +
"(select OID and Option FROM DBOption WHERE OID='" + oid.ToString() + "' and Option='" + option.ToString() + "')";
var quizOption = objConn.Prepare(InsertOption);
quizOption.Step();
}
}
}
this.Loaded += ReadTryoutList_Loaded;
loading.IsIndeterminate = false;
}
}
private void ReadTryoutList_Loaded(object sender, RoutedEventArgs e)
{
ReadAllDBName dbName = new ReadAllDBName();
DB_TryoutList = dbName.GetAllDBName();
ListTryout.ItemsSource = DB_TryoutList.OrderByDescending(i => i.ID).ToList();//Binding DB data to LISTBOX and Latest contact ID can Display first.
if (DB_TryoutList.Count == 0)
{
statuskosongStack.Visibility = Visibility.Visible;
ListTryout.Visibility = Visibility.Collapsed;
}
else
{
statuskosongStack.Visibility = Visibility.Collapsed;
ListTryout.Visibility = Visibility.Visible;
}
}
我遇到了问题,即当用户选择上一页上的列表并且数据已成功存储在数据库中时,但listview上的数据量不会增加。如果我单击后退按钮并再次在此页面上输入,则listview上的数据会增加。如何在成功保存数据时使listview增加数据量?
答案 0 :(得分:0)
此问题是由代码中的异步方法引起的。单击该项后,导航到TryoutLibrary1
页面。在此页面上,您将数据保存到数据库,同时从Page.Loaded
事件中获取数据库中的数据源。但是当您获得数据时,单击的项目尚未插入到数据库中,因此在您重新加载数据之前它不会显示在页面上(再次重新输入页面)。
因此,您应该在ItemClick
事件中重新考虑您的逻辑,您应该确保数据已保存到数据库中,然后您将数据设置为ListView.ItemsSource
。例如,当您单击该项时,您可以创建一个对象并将其放入本地ObservableCollection
对象,然后将ObservableCollection
设置为ListView itemsSource以使其显示在UI上,在其他您可以将项目保存到数据库中。