如何在 Titanium 中使用 SQLite 加载 ListView?
How to load a ListView using SQLite in Titanium?
我实际上在使用框架Alloy。我在我的模型 .js 中试过这个:
var myBooks = Alloy.Collections.books;
Var getData = myBooks.fetch();
但是,要设置 ListView,它需要获取数组类型,老实说,我只知道使用 getData 变量我得到了一个 Backbone.Object 类型的对象,但我不知道如何映射它。
提前感谢您的帮助。
这不是为 Alloy 编写的,而是即插即用的代码。
// Create main window
var win = Ti.UI.createWindow({
backgroundColor:"#bbb",
navBarHidden : true,
orientationModes : [Titanium.UI.PORTRAIT]
});
// Install the database
var db = Ti.Database.install("myDB.sqlite", "myDB");
db.close();
// Create the empty array
var myBooks = [];
// Set data in the app
function setData(){
var db = Ti.Database.open("myDB");
// Select the "books" table in the database
var rows = db.execute("SELECT * FROM books");
while (rows.isValidRow()) {
// Add each "title" under the books table to the array
myBooks.push(rows.fieldByName("title"));
rows.next();
}
rows.close();
db.close();
// Print the array
Ti.API.info(myBooks);
}
// call the function
setData();
win.open();
刚刚用sqlite做了一个简单的数据库,有一个table"books",然后在books下"titles"。在 "books" 中,我添加了三个值,"Book One"、"Book Two" 和 "Book Three"。
从这里您应该可以使用 myBooks 数组添加到 Alloy 中的 ListView。
我找到了答案,不需要将值解析为数组并调用行:
$.elementsOnList.section[0].setItems(books);
代替你可以简单地:
//Get a reference to our collection:
Alloy.Collections.books.fetch();
并且在视图 .js 中利用通过其框架提供 Titanium 的属性 Alloy:
<Alloy>
<Collection src="books" />
<Window>
<ListView defaultItemTemplate="by_name">
<Templates>
<ItemTemplate name="by_name">
<View>
<Label bindId="title"/>
</View>
</ItemTemplate>
</Templates>
<ListSection dataCollection="books">
<ListItem title:text="{title}" />
</ListSection>
</ListView>
</Window>
</Alloy>
通过那几行代码,我的列表正在从数据库中加载数据。
我实际上在使用框架Alloy。我在我的模型 .js 中试过这个:
var myBooks = Alloy.Collections.books;
Var getData = myBooks.fetch();
但是,要设置 ListView,它需要获取数组类型,老实说,我只知道使用 getData 变量我得到了一个 Backbone.Object 类型的对象,但我不知道如何映射它。
提前感谢您的帮助。
这不是为 Alloy 编写的,而是即插即用的代码。
// Create main window
var win = Ti.UI.createWindow({
backgroundColor:"#bbb",
navBarHidden : true,
orientationModes : [Titanium.UI.PORTRAIT]
});
// Install the database
var db = Ti.Database.install("myDB.sqlite", "myDB");
db.close();
// Create the empty array
var myBooks = [];
// Set data in the app
function setData(){
var db = Ti.Database.open("myDB");
// Select the "books" table in the database
var rows = db.execute("SELECT * FROM books");
while (rows.isValidRow()) {
// Add each "title" under the books table to the array
myBooks.push(rows.fieldByName("title"));
rows.next();
}
rows.close();
db.close();
// Print the array
Ti.API.info(myBooks);
}
// call the function
setData();
win.open();
刚刚用sqlite做了一个简单的数据库,有一个table"books",然后在books下"titles"。在 "books" 中,我添加了三个值,"Book One"、"Book Two" 和 "Book Three"。
从这里您应该可以使用 myBooks 数组添加到 Alloy 中的 ListView。
我找到了答案,不需要将值解析为数组并调用行:
$.elementsOnList.section[0].setItems(books);
代替你可以简单地:
//Get a reference to our collection:
Alloy.Collections.books.fetch();
并且在视图 .js 中利用通过其框架提供 Titanium 的属性 Alloy:
<Alloy>
<Collection src="books" />
<Window>
<ListView defaultItemTemplate="by_name">
<Templates>
<ItemTemplate name="by_name">
<View>
<Label bindId="title"/>
</View>
</ItemTemplate>
</Templates>
<ListSection dataCollection="books">
<ListItem title:text="{title}" />
</ListSection>
</ListView>
</Window>
</Alloy>
通过那几行代码,我的列表正在从数据库中加载数据。