将适配器从膨胀的XML设置为ListView以打印PDF-缺少项目

问题描述 投票:0回答:1

我正在尝试通过膨胀的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(通过更改backgroundcolor进行了检查),但没有任何项目。调试ArrayAdapter显示,设置适配器后未调用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中的任何内容。由于数据库调用,因此从异步任务中调用了代码。

android pdf view android-listview
1个回答
0
投票

问题是高度和宽度的计算。

需要完成AFTER设置ArrayAdapter。

热门问题
推荐问题
最新问题