1

所以我是 BB 开发、QML 和 C++ 领域的新手,所以在我尝试解释我的问题时请多多包涵。

我正在制作一个应用程序,该应用程序将从某个遥远的 API 中搜索人们,该 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 和将其直接推送到数据源对象的源变量,但两种方式都不'似乎没有工作。我究竟做错了什么?这让我发疯,因为我无法弄清楚,但这似乎是一个简单的答案。


编辑:好的,这真是太不直观了!因此,我只需在主页的“推送”对象下添加 theUrl:“链接到 url”,然后在顶部的第二页添加“属性字符串 theUrl:乱码”,就可以修复它。但是现在我正在尝试使 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
        }
    ]
}

所以我目前正在使用两个不同的 url 进行测试:url1 和 url2。url1设置为test,test设置为推送下的Url。这很好,但是当我添加行 push.theUrl = url2; 该页面仍然返回 url1 的结果。所以我所做的是添加行 text = push.theUrl; 出于测试目的,其中“text”是按钮上显示的文本,当我运行应用程序时,即使它没有返回给我一个页面,“text”实际上设置为 url2,这意味着 push.theUrl 是 url2。到目前为止,这一切都非常不直观,而且根本不是一次愉快的体验。我在这里尝试做的事情(从文本字段获取输入,将其添加到链接并将其发送到另一个页面)在 Android 上最多需要一个小时来编写代码。

4

0 回答 0