Search code examples
androidpdfviewandroid-listview

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


I'm trying to create a PDF from an inflated xml layout. Within this layout there is a ListView which I want to fill with items and then print everything to PDF.

Here is the code:

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

Here is the parent XML including the 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>

Here ist the XML for the items:

<?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>

And the 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;
}

}

I get an A4 page with the Headline-Text including the empty ListView (checked it by changing backgroundcolor) but without items. Debugging the ArrayAdapter shows that getView() is not called after setting the Adapter.

changing the ListView to a LinearLayout and adding the items with:

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));
                }

results in calling getView but does not change anything in the pdf. The code is called from an async task because of the database call.


Solution

  • The problem was the calculation of height and width.

    It needs to be done AFTER setting the ArrayAdapter.