所以我是BB开发,QML和C ++的新手,所以在我尝试解释我的问题时请耐心等待。
我正在制作一个应用程序,它将从一些远程API中查找返回XML文件的人,然后我将解析并在列表中显示信息。
所以在第一页中我有两个带有名字和姓氏的文本字段。我还有一个转到结果页面的按钮,并且还应该发送用于联系API的URL。这是第一页的代码:
import bb.cascades 1.0
NavigationPane {
id: nav
Page {
content: Container {
TextField {
id: firstName
hintText: "First Name"
inputMode: TextFieldInputMode.Text
input {
flags: TextInputFlag.PredictionOff | TextInputFlag.AutoCorrectionOff
}
validator: Validator {
id: firstNameValidator
mode: ValidationMode.Immediate
errorMessage: "You must enter at least three characters."
onValidate: {
if (firstName.text.length >= 3 || firstName.text.length == 0)
state = ValidationState.Valid;
else
state = ValidationState.Invalid;
}
}
}
TextField {
id: lastName
hintText: "Last Name"
inputMode: TextAreaInputMode.Text
input {
flags: TextInputFlag.PredictionOff | TextInputFlag.AutoCorrectionOff
onSubmitted: {
if (firstNameValidator.state == ValidationState.Valid
&& lastNameValidator.state == ValidationState.Valid) {
theButton.text = "Valid enter!"
} else {
theButton.text = "Invalid enter!!!!"
}
}
}
validator: Validator {
id: lastNameValidator
mode: ValidationMode.Immediate
errorMessage: "You must enter at least three characters."
onValidate: {
if (lastName.text.length >= 3 || lastName.text.length == 0)
state = ValidationState.Valid;
else
state = ValidationState.Invalid;
}
}
}
Button {
id: theButton
text: "Search"
onClicked: {
if (firstNameValidator.state == ValidationState.Valid
&& lastNameValidator.state == ValidationState.Valid) {
text = "Valid button press!"
pushed.theUrl = link to url
pushed.dataSource.source = link to url
nav.push(pushed)
} else {
text = "Invalid button press!!!"
}
}
}
}
}
attachedObjects: [
AdvancedResult {
id: pushed
}
]
}
这是第二页的代码:
import bb.cascades 1.0
import bb.data 1.0
Page {
property string theUrl
property alias dataSource: dataSource
content: ListView {
id: myListView
// Associate the list view with the data model that's defined in the
// attachedObjects list
dataModel: dataModel
listItemComponents: [
ListItemComponent {
type: "item"
// Use a standard list item to display the data in the data
// model
StandardListItem {
imageSpaceReserved: false
title: ListItemData.FIRST + " " + ListItemData.LAST
description: ListItemData.JOB
status: ListItemData.PHONE
}
}
]
onTriggered: {
var selectedItem = dataModel.data(indexPath);
// Do something with the item that was tapped
}
}
attachedObjects: [
GroupDataModel {
id: dataModel
grouping: ItemGrouping.None
},
DataSource {
id: dataSource
// Load the XML data from a remote data source, specifying that the
// "item" data items should be loaded
source: theUrl
query: some query
type: DataSourceType.Xml
onDataLoaded: {
// After the data is loaded, clear any existing items in the data
// model and populate it with the new data
dataModel.clear();
if (data[0] == undefined) {
dataModel.insert(data);
} else {
dataModel.insertList(data);
}
}
}
]
onCreationCompleted: {
// When the top-level Page is created, direct the data source to start
// loading data
dataSource.load();
}
}
正如您在第一页中看到的,我尝试以两种不同的方式附加数据源对象的源:通过将其直接推送到全局字符串theURL并将其直接推送到数据源对象的源变量但是两种方式似乎都不起作用。我究竟做错了什么?这让我发疯,因为我还没弄清楚它似乎是一个如此简单的答案。
编辑:好的,这非常不直观!所以我能够通过简单地在主页面中的“推送”对象下添加url:“link to url”并在顶部的第二页添加“属性字符串theUrl:gibberish”来修复它。但是现在我正在尝试将其设置为根据用户输入而不同的URL,它只是不起作用。
所以这是我目前的代码:
import bb.cascades 1.0
NavigationPane {
id: nav
property string firstName: ""
property string lastName: ""
property string jobTitle: ""
property string test: url1
Page {
content: Container {
TextField {
id: firstNameField
hintText: "First Name"
inputMode: TextFieldInputMode.Text
input {
flags: TextInputFlag.PredictionOff | TextInputFlag.AutoCorrectionOff
}
validator: Validator {
id: firstNameValidator
mode: ValidationMode.Immediate
errorMessage: "You must enter at least three characters."
onValidate: {
if (firstNameField.text.length >= 3 || firstNameField.text.length == 0) {
state = ValidationState.Valid;
firstName = firstNameField.text;
} else {
state = ValidationState.Invalid;
}
}
}
}
TextField {
id: lastNameField
hintText: "Last Name"
inputMode: TextAreaInputMode.Text
input {
flags: TextInputFlag.PredictionOff | TextInputFlag.AutoCorrectionOff
onSubmitted: {
if (firstNameValidator.state == ValidationState.Valid
&& lastNameValidator.state == ValidationState.Valid) {
theButton.text = "Valid enter!"
} else {
theButton.text = "Invalid enter!!!!"
}
}
}
validator: Validator {
id: lastNameValidator
mode: ValidationMode.Immediate
errorMessage: "You must enter at least three characters."
onValidate: {
if (lastNameField.text.length >= 3 || lastNameField.text.length == 0) {
state = ValidationState.Valid;
lastName = lastNameField.text;
} else {
state = ValidationState.Invalid;
}
}
}
}
Button {
id: theButton
text: "Search"
onClicked: {
if (firstNameValidator.state == ValidationState.Valid
&& lastNameValidator.state == ValidationState.Valid) {
//text = "Valid button press!"
pushed.theUrl = url2
text = pushed.theUrl;
nav.push(pushed)
} else {
text = "Invalid button press!!!"
}
}
}
}
}
attachedObjects: [
AdvancedResult {
id: pushed
theUrl: test
}
]
}
所以我目前正在使用两个不同的网址进行测试:url1和url2。 url1设置为test并且test被设置为theUrl。这工作正常,但当我添加行push.theUrl = url2;该页面仍然返回url1的结果。所以我做的是添加行text = pushing.theUrl;出于测试目的,“text”是按钮上显示的文本,当我运行应用程序时,即使它没有返回给我一个页面,“text”实际上设置为url2意味着push.theUrl是url2。这一切都非常不直观,到目前为止还没有令人愉快的体验。我在这里尝试做什么(从文本字段获取输入,将其添加到链接并将其发送到另一个页面)在Android上最多花了我一个小时代码。