2

I've been trying to draw a listView to a canvas so I can create a PDF document out of it, but have been unsuccessful. The problem is that my listView is longer than my screen, so only 6 elements out of the 9 are actually on screen, and only those 6 get drawn.

I've tried everything I can think of, but nothing seems to work. How do I get the entire listView so that I can draw it to a canvas?

Here is the XML of the listView:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/cl_lv_k"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="@null"
        android:dividerHeight="0dp" >
    </ListView>
    ....

When I try and access the listView by findViewById(R.id.cl_lv_k) it only gives me the current elements that are on the screen.

I've tried everything I can think of. Help would be greatly appreciated.

EDIT:

I got a solution. Thanks to @Mike M. for finding a related question: Android get screenshot of all ListView items

I used the following code in my program, and it worked.

ListView listView = (ListView) findViewById (R.id.cl_lv_k);
PageInfo pageInfo = 
                new PageInfo.Builder(pageWidth, 12000, 1).create();
Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();

ChartAdapter adapter = listView.getAdapter(); //this is just a custom adapter
for (int i = 0; i < adapter.size(); i++)
{
    View childView = adapter.getView(i, null, kColumn);
            childView.measure(MeasureSpec.makeMeasureSpec(kColumn.getWidth(),
                                                  MeasureSpec.EXACTLY),
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    childView.layout(0, 0, childView.getMeasuredWidth(),     
                                    childView.getMeasuredHeight());
    childView.setDrawingCacheEnabled(true);
    childView.buildDrawingCache();
    childView.getDrawingCache();
    childView.draw(canvas);
    canvas.translate(0, childView.getMeasuredHeight());
}
....

I made a PDF document out of this page, so I filled in the rest of the PDF code and it worked.

Community
  • 1
  • 1
Andrew
  • 31
  • 7
  • I've not tried this, but I would imagine you could iterate over the number of list items, calling `getView()` on the Adapter for each, and drawing them to the Canvas sequentially. – Mike M. Aug 21 '14 at 15:34
  • @Mike M. I've tried that, sort of. I couldn't figure out how to use the `getView(int position, View convertView, ViewGroup parent)` because I couldn't figure out the arguments (aside from `int position`). – Andrew Aug 21 '14 at 16:02
  • You can pass null for the `convertView`, and the ListView itself for the `parent` ViewGroup. – Mike M. Aug 21 '14 at 16:09
  • I can't get it to work. It seems to grab the view just fine, but no matter what I try, nothing shows up when I go to draw it. – Andrew Aug 21 '14 at 23:45
  • I'd've figured someone would've answered by now. I'll see if I can get something working when I get some free time in a little while. As I said, I've not tried it; it was pure speculation. I'll let you know what I find. – Mike M. Aug 22 '14 at 00:04
  • You might check this question: http://stackoverflow.com/questions/12742343/android-get-screenshot-of-all-listview-items – Mike M. Aug 22 '14 at 00:11

0 Answers0