android - 从膨胀的 XML 将适配器设置为 ListView 以打印 PDF - 缺少项目
问题描述
我正在尝试从膨胀的 xml 布局创建 PDF。在这个布局中有一个 ListView,我想用项目填充,然后将所有内容打印到 PDF。
这是代码:
List<PersonData> personDataList = getListfromDataBase();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.pdf_print_container, null);
//Start new PDF-Document
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(595, 842, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
//https://stackoverflow.com/questions/43699638/android-create-and-print-pdf-from-layout-view
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
view.measure(measureWidth, measuredHeight);
view.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());
//get the ListView where to add the items
ListView listView = view.findViewById(R.id.pdf_listview_for_items);
//Create the Adapter
PdfArrayAdapter pdfArrayAdapter = new PdfArrayAdapter(view.getContext(), personDataList);
//set the adapter to the listView
listView.setAdapter(pdfArrayAdapter);
//Draw the view to the pdf page, and write it
view.draw(page.getCanvas());
document.finishPage(page);
document.writeTo(new FileOutputStream(filePath + ".pdf"));
这是包含 ListView 的父 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">
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Page Headline and some more Information"/>
<ListView
android:id="@+id/pdf_listview_for_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
这是项目的 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="wrap_content">
<TextView
android:id="@+id/pdf_print_station_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<!-- and some more text views-->
</LinearLayout>
和 ArrayAdapter:
public class PdfArrayAdapter extends ArrayAdapter {
public PdfArrayAdapter(Context context, List <PersonData> personDataList) {
super(context, 0, personDataList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.pdf_print_item, parent, false);
}
PersonData personData = (PersonData) getItem(position);
TextView stationone = convertView.findViewById(R.id.pdf_print_station_one);
stationone.setText(personData.getStationonetime());
//And some more TextViews
return convertView;
}
}
我得到一个带有标题文本的 A4 页面,包括空的 ListView(通过更改背景颜色进行检查)但没有项目。调试 ArrayAdapter 显示设置 Adapter 后未调用 getView()。
将 ListView 更改为 LinearLayout 并添加以下项目:
LinearLayout linearlayout = view.findViewById(R.id.pdf_linerlayout_for_items);
for (int i = 0; i < pdfArrayAdapter.getCount(); i++) {
linearlayout.addView(pdfArrayAdapter.getView(i, null, linearlayout));
}
导致调用 getView 但不会更改 pdf 中的任何内容。由于数据库调用,代码是从异步任务中调用的。
解决方案
问题是高度和宽度的计算。
它需要在设置 ArrayAdapter之后完成。
推荐阅读
- makefile - 如何从标准输入读取到生成文件变量
- java - 如何为另一个构造函数的类中的类创建字段数组引用
- python - 从列创建列表,保留重复项
- javascript - 如何从字符串中找到 EJS 标签变量名?
- javascript - 函数内的返回语句返回未定义
- dialogflow-es - Dialogflow - 获取安全问题的答案
- python - NameError: name 'e' is not defined, 如何计算正确
- php - 在 RHEL 7.4 和 PHP 7.3 上安装 RPM php_mbstring | 调用未定义函数 mb_substr()
- uima - 使用 ruta,注释包含注释的行并提取所需数据
- ruby-on-rails - 如何在ruby中动态创建带参数的方法?