将 Adapter 从 inflated XML 设置为 ListView 以打印 PDF - 缺少项目

Set Adapter to ListView from inflated XML for printing PDF - missing items

我正在尝试从膨胀的 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);

//
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"));

这里是 parent XML,包括 ListView:

<?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 页面,其中 Headline-Text 包括空的 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 之后完成。