2017-09-01 64 views
0

我正在開發打開epub文件的圖書閱讀器。我在因特網上搜索了很多,最後在此tutorial link中找到了解決方案。Android(Java) - 通過遞歸方法填充ArrayList


但現在我遇到的麻煩與方法

logTableOfContents(名單tocReferences,詮釋深度)

我mofided從無效到字符串,它是我的代碼

private String logTableOfContents(List<TOCReference> tocReferences, int depth) { 

    if (tocReferences == null) { 




     return null; 
    } 

    for (TOCReference tocReference : tocReferences) { 
     StringBuilder tocString = new StringBuilder(); 
     for (int i = 0; i < depth; i++) { 
      tocString.append("\t"); 
     } 

     tocString.append(tocReference.getTitle()); 

     //Toast.makeText(this, ""+ logTableOfContents(tocReference.getChildren(), depth + 1), Toast.LENGTH_SHORT).show(); 

     return tocString.toString(); 

    } 

    return null; 
} 

可以清楚地看到,編寫該教程的開發人員已經使用遞歸方法來獲得目錄


我改變方法無效爲String獲取並存儲在ArrayList中 ,但是試圖登錄ArrayList中的元素時,不能單獨獲得的內容本書的表外遞歸method.Got NullPointerException異常的。


現在我想從某人的幫助解決這個問題,任何建議將受到歡迎,在此先感謝。從tutorial


代碼,我使用

import java.io.IOException; 
 
import java.io.InputStream; 
 
import java.util.List; 
 
    
 
import nl.siegmann.epublib.domain.Book; 
 
import nl.siegmann.epublib.domain.TOCReference; 
 
import nl.siegmann.epublib.epub.EpubReader; 
 
import android.app.Activity; 
 
import android.content.res.AssetManager; 
 
import android.graphics.Bitmap; 
 
import android.graphics.BitmapFactory; 
 
import android.os.Bundle; 
 
import android.util.Log; 
 
    
 
/** 
 
* Log the info of 'assets/books/testbook.epub'. 
 
* 
 
* @author paul.siegmann 
 
* 
 
*/ 
 
public class LogTestBookInfo extends Activity { 
 
    /** Called when the activity is first created. */ 
 
    @Override 
 
    public void onCreate(Bundle savedInstanceState) { 
 
    super.onCreate(savedInstanceState); 
 
    setContentView(R.layout.main); 
 
    AssetManager assetManager = getAssets(); 
 
    try { 
 
     // find InputStream for book 
 
     InputStream epubInputStream = assetManager 
 
      .open("books/testbook.epub"); 
 
    
 
     // Load Book from inputStream 
 
     Book book = (new EpubReader()).readEpub(epubInputStream); 
 
    
 
     // Log the book's authors 
 
     Log.i("epublib", "author(s): " + book.getMetadata().getAuthors()); 
 
    
 
     // Log the book's title 
 
     Log.i("epublib", "title: " + book.getTitle()); 
 
    
 
     // Log the book's coverimage property 
 
     Bitmap coverImage = BitmapFactory.decodeStream(book.getCoverImage() 
 
      .getInputStream()); 
 
     Log.i("epublib", "Coverimage is " + coverImage.getWidth() + " by " 
 
      + coverImage.getHeight() + " pixels"); 
 
    
 
     // Log the tale of contents 
 
     logTableOfContents(book.getTableOfContents().getTocReferences(), 0); 
 
    } catch (IOException e) { 
 
     Log.e("epublib", e.getMessage()); 
 
    } 
 
    } 
 
    
 
    /** 
 
    * Recursively Log the Table of Contents 
 
    * 
 
    * @param tocReferences 
 
    * @param depth 
 
    */ 
 
    private void logTableOfContents(List<TOCReference> tocReferences, int depth) { 
 
    if (tocReferences == null) { 
 
     return; 
 
    } 
 
    for (TOCReference tocReference : tocReferences) { 
 
     StringBuilder tocString = new StringBuilder(); 
 
     for (int i = 0; i < depth; i++) { 
 
     tocString.append("\t"); 
 
     } 
 
     tocString.append(tocReference.getTitle()); 
 
     Log.i("epublib", tocString.toString()); 
 
    
 
     logTableOfContents(tocReference.getChildren(), depth + 1); 
 
    } 
 
    } 
 
}


,我用的教程已經寫庫。

回答

0

我剛剛爲我的問題建立了解決方案。


我添加了一個新的參數,以我的logTableOfContent(...,...,... ArrayList的)方法,並修改了它:


private void logTableOfContents(List<TOCReference> tocReferences, int depth, ArrayList<String> ListOfContents) { 
 

 
     if (tocReferences == null) { 
 

 

 
      return; 
 
     } 
 

 
     for (TOCReference tocReference : tocReferences) { 
 
      StringBuilder tocString = new StringBuilder(); 
 
      for (int i = 0; i < depth; i++) { 
 
       tocString.append("\t"); 
 
      } 
 

 
      tocString.append(tocReference.getTitle()); 
 

 
      Log.v("TAG", tocString.toString()); 
 

 
      ListOfContents.add(tocString.toString()); 
 

 
      logTableOfContents(tocReference.getChildren(), depth + 1, ListOfContents); 
 

 
      //return tocString.toString(); 
 

 
     } 
 

 
    }


無效法可以用作

ArrayList<String> TableOfContent = new ArrayList<>(); 

     logTableOfContents(book.getTableOfContents().getTocReferences(), 0, TableOfContent); 

     for (String string:TableOfContent){ 

      Toast.makeText(this, ""+ string, Toast.LENGTH_SHORT).show(); 

     } 

感謝很多那些誰抽出時間來審查這個問題。