2013-07-29 22 views
0

我試圖通過點擊事件瀏覽我的音樂目錄 - 我可以獲得原始列表視圖,顯示可以開始,單擊事件將向下鑽取到下一個目錄,但在第三次單擊我的索引位置不斷返回原始directoryEntry對象的完整路徑。Phonegap:通過目錄瀏覽,索引位置不正確

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); 
      } 


    }); 
} 

回答

0

我設法使用下面的代碼得到這個工作。我確定它有點混亂,但如果他們陷入同一問題,希望能幫助別人。基本上這將通過閱讀我的音樂根文件夾中的第一個目錄開始,然後當每個列表目錄項被點擊時,它將刷新子目錄,文件等。

我必須將$(document),on 「點擊」.....)到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); 


    });