Android(Java) - 通过递归方法填充ArrayList

时间:2017-09-01 09:28:33

标签: java android recursion arraylist

我正在开发打开epub文件的图书阅读器。我在互联网上搜索了很多,最后在tutorial link找到了解决方案。

但现在我遇到了方法

的问题

logTableOfContents(列出tocReferences,int深度)

我将它从void转换为String,它是我的代码

 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。

现在我希望得到某人的帮助来解决这个问题,欢迎任何建议,提前致谢。

我使用的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);
    }
  }
}

我使用的库已在教程中编写。

1 个答案:

答案 0 :(得分: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();

        }

    }

void方法可以用作

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

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

        for (String string:TableOfContent){

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

        }

感谢那些没有时间审查这个问题的人。