Phonegap:在目录中导航,索引位置不正确
Phonegap: navigating through directories, index position is incorrect
我正试图使用单击事件在我的音乐目录中导航-我可以显示原始列表视图。一开始,单击事件会深入到下一个目录,但在第三次单击时,我的索引位置会不断返回原始directoryentry对象的fullPath。
JS-
function readerSuccess(entries) {
$("#test").empty();
for (i=0;i<entries.length;i++){
$("#test").append("<li>"+entries[i].name+"</li>");
}
$("#test").listview("refresh");
ListClickHandler(entries);
}
var ListClickHandler = function(something){
$(document).on("click","#test li",function(event){
var mylistname = $(this).text();
var index = $("#test li").index(this);
var listpath = something[index].fullPath; //this is causing the problem. entries never changes.
alert(index);
if(something[index].isFile ===true)
{
$("#test").empty();
alert("this is a file");
}
else if(something[index].isDirectory===true)
{
var directoryentry = new DirectoryEntry(mylistname,listpath);
var directoryreader = directoryentry.createReader();
directoryreader.readEntries(readerSuccess,fail);
//alert("this is a directory"+mylistname+listpath);
}
});
}
我使用以下代码成功地实现了这一点。我确信这有点混乱,但如果有人在同样的问题上被难住了,希望能帮助他们。基本上,这将从读取我的音乐根文件夹中的第一个目录开始,然后当单击每个列表目录项时,它将刷新子目录、文件等。
我不得不将$(document),on("click"…..)移动到readerSuccess函数中,以便在触发列表单击事件时,它正在拾取正确的列表条目。我还添加了两个新数组来包含entries.name和entries.fullPath,它们在每次迭代时都会被清空,并用正确的数据重新填充。下面的代码是我如何让它工作的,但我相信还有更好的方法。我绝望地在谷歌上搜索后什么也找不到。
function readerSuccess(entries) {
$("#test").empty();
var listnames = new Array();
var listpositions = new Array();
for (i=0;i<entries.length;i++){
if (entries[i].isDirectory===true)
{
alert("this is a directory");
}
if (entries[i].isFile===true){
alert("this is a file")
}
$("#test").append("<li>"+entries[i].name+"</li>");
listnames.push(entries[i].name);
listpositions.push(entries[i].fullPath);
}
$("#test").listview("refresh");
$(document).on("click","#test li",function(event){
var mylistname = $(this).text();
//alert(mylistname);
var index = $("#test li").index(this);
//alert(index);
var listpath = listpositions[index];
//alert(listpath);
listnames.length=0; //resets the array so new data appends with correct pos
listpositions.length=0; //resets the array so new data appends with correct pos
var directoryentry= new DirectoryEntry(mylistname,listpath);
var directoryreader = directoryentry.createReader();
directoryreader.readEntries(readerSuccess,fail);
});
相关文章:
- 在从索引位置返回的字符串中查找空白
- 需要呈现MongoDB查询返回结果的特定索引/位置
- 有没有办法得到引导3旋转木马的索引位置
- 点击JQuery/HTML5显示元素的索引/位置
- Javascript i(index,iterator):如果用作变量,它将始终被解释为索引位置
- 获取所选元素的位置,而不是 DOM 中元素的索引
- Javascript 返回数组中“匹配”数组的位置索引
- 动态 jquery ui 可排序块的索引/位置
- Windows 8 应用程序中的索引数据库位置
- 如何删除字符串中的索引位置范围
- 工具提示 z 索引位置或其他内容
- jquery附加到索引位置
- 特殊字符的javascript验证's索引/位置
- Phonegap:在目录中导航,索引位置不正确
- 如何在AngularJS中保存对象的JSON索引位置,而不是对象本身
- 将数组元素按索引位置相乘
- 返回匿名函数数组的索引位置
- 哪个值是javascript数组.indexOf作为索引位置
- 如何通过文本搜索返回JSON数组中项目的索引位置?
- 按索引位置解析json对象数组