Phonegap:在目录中导航,索引位置不正确

Phonegap: navigating through directories, index position is incorrect

本文关键字:索引 位置 不正确 导航 Phonegap      更新时间:2023-09-26

我正试图使用单击事件在我的音乐目录中导航-我可以显示原始列表视图。一开始,单击事件会深入到下一个目录,但在第三次单击时,我的索引位置会不断返回原始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);

    });