[英]Java Recursive Method ArrayList is not adding all child elements to the final ArrayList
[英]Android (Java) - Fill ArrayList by means of recursive method
我正在開發可打開epub文件的閱讀器。我在互聯網上進行了大量搜索,最后在本教程鏈接中找到了解決方案。
但是現在我在使用該方法時遇到了麻煩
logTableOfContents(列出tocReferences,int深度)
我把它從空改成字符串,這是我的代碼
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;
}
可以清楚地看到,准備該教程的開發人員已使用遞歸方法來獲取目錄 。
我將方法void更改為String以獲取並將它們存儲在ArrayList中,但是無法從遞歸方法中單獨獲取Book的目錄。嘗試記錄ArrayList的元素時出現nullPointerException。
現在,我想尋求某人的幫助來解決此問題,歡迎提出任何建議,謝謝。
我使用的教程中的代碼
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); } } }
我使用的庫已在本教程中編寫。
我剛剛為我的問題找到了解決方案。
我向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(); } }
void方法可以用作
ArrayList<String> TableOfContent = new ArrayList<>();
logTableOfContents(book.getTableOfContents().getTocReferences(), 0, TableOfContent);
for (String string:TableOfContent){
Toast.makeText(this, ""+ string, Toast.LENGTH_SHORT).show();
}
非常感謝那些抽出時間來審查問題的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.