这是我的代码(MainActivity1.java)
package com.example.folowreader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubReader;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity1 extends ListActivity {
private LayoutInflater inflater;
private List<RowData> contentDetails;
Intent intent ;
public static final String BOOK_NAME = "austen-pride-and-prejudice-illustrations.epub";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
contentDetails = new ArrayList<RowData>();
// myClass((Context) this.getActivity());
AssetManager assetManager = getAssets();
try {
InputStream epubInputStream = assetManager.open(BOOK_NAME);
Book book = (new EpubReader()).readEpub(epubInputStream);
logContentsTable(book.getTableOfContents().getTocReferences(), 0);
} catch (IOException e) {
Log.e("epublib", e.getMessage());
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.main2,
R.id.textView3, contentDetails);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
private class CustomAdapter extends ArrayAdapter<RowData>{
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super((Context) context, resource, textViewResourceId, objects);
}
private class ViewHolder{
private View row;
private TextView titleHolder = null;
public ViewHolder(View row) {
super();
this.row = row;
}
public TextView getTitle() {
if(null == titleHolder)
titleHolder = (TextView) row.findViewById(R.id.textView3);
return titleHolder;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
RowData rowData = getItem(position);
if(null == convertView){
convertView = inflater.inflate(R.layout.main2, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.getTitle();
title.setText(rowData.getTitle());
return convertView;
}
}
private void logContentsTable(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());
RowData row = new RowData();
row.setTitle(tocString.toString());
row.setResource(tocReference.getResource());
contentDetails.add(row);
logContentsTable(tocReference.getChildren(), depth + 1);
}
}
private class RowData{
private String title;
private Resource resource;
public RowData() {
super();
}
public String getTitle() {
return title;
}
public Resource getResource() {
return resource;
}
public void setTitle(String title) {
this.title = title;
}
public void setResource(Resource resource) {
this.resource = resource;
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
RowData rowData = contentDetails.get(position);
intent = new Intent(MainActivity1.this, ContentViewActivity.class);
try {
intent.putExtra("display", new String(rowData.getResource().getData()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startActivity(intent);
}
这是我的 ContentViewActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_view);
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
String displayString = getIntent().getExtras().getString("display");
if(displayString != null)
webView.loadData(displayString, "text/html", "utf-8");
}
这是 MainActivity1 的 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ADD8E6"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.folowreader.MainActivity1" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView5"
android:layout_marginLeft="75dp"
android:layout_marginTop="25dp"
android:layout_toRightOf="@+id/textView5" >
</ListView>
</RelativeLayout>
这是 ContentViewActivity 的 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.folowreader.ContentViewActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight = "true"
></WebView>
</RelativeLayout>
这是 main2 的 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ADD8E6" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
我试图通过添加路径和书名来更改书名,但什么也没发生。这本书没有显示。该代码旨在制作内容列表(目录),然后在 webView 内的单独活动中显示每一行的内容。请帮助我,我需要帮助。我需要知道如何使代码工作。