我想将ListView中的所有项目保存到pdf文件

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

我编写了一个条形码阅读器程序,在列表视图中显示条形码

无论我如何努力,我都无法通过按按钮将监视列表项目保存到 PDF 文件,请帮助我。

@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity  implements View.OnClickListener {

    Button scanBtn, btnok;

    TextView  messageFormat,ragham ;
    ArrayList<String> barcodelist = new ArrayList<>();

    HashSet<String> hashSet = new HashSet<String>();

    ArrayAdapter <String> adapter;
    String filename = "";
    String filepath = "";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // referencing and initializing
        // the button and textviews
        scanBtn = findViewById(R.id.btnscann);
        filename = "barcodes.txt";
        ListView list1 = findViewById(R.id.list1);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,barcodelist);

        list1.setAdapter(adapter);



        scanBtn.setOnClickListener(this);



        Button b=findViewById(R.id.btnok);
        btnok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();


                ObjectOutputStream out;
               Object[] objs = new Object[list1.getCount()];
               for (int i = 0 ; i < list1.getCount();i++) {
                  Object obj = (Object)list1.getItemAtPosition(i);
                  objs[i] = obj;
               }                try {
                   out = new ObjectOutputStream
                           (new FileOutputStream(new File(filename)));
                   out.writeObject(objs);
                   out.flush();
                    out.close();
                } catch (FileNotFoundException e) {
                   e.printStackTrace();
               } catch (IOException e) {
                    e.printStackTrace();
               }


            }
});
java android listview save export
1个回答
0
投票

现在确实不应该使用列表视图,recyclerview 在屏幕上显示效率更高,但您可以使用 PDFDocument 将视图直接绘制到 PDF,列表视图或 recyclerview 可以创建非标准 PDF 页面尺寸将所有内容放入一页中,否则您必须弄清楚如何将项目拆分到多个页面上。

我已经完成了一个关于如何将 recyclerview 绘制到多页 pdf 的完整示例

© www.soinside.com 2019 - 2024. All rights reserved.