java - 从 ListView 到 PDF 的数据

标签 java android listview

我想生成一个包含 ListView 中所有数据的 PDF。我正在使用包含以下列的自定义 ListView :id、estudante、hora。
我只能将estudante列数据写入PDF,但无法返回id列和hora列。 我想以有组织的方式列出它,但我只能将它放在一行上。

Class listing the records:
public class ListarAlunosActivity extends AppCompatActivity {

ListView lista;
public AlunoDAO dao;
public List<Aluno> alunos;
public List<Aluno>alunosFiltrados = new ArrayList<>();

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

        lista = findViewById(R.id.listv);
        dao = new AlunoDAO(this);
        alunos = dao.obterTodos();
        alunosFiltrados.addAll(alunos);
        //ArrayAdapter<Aluno> adaptador = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, alunos);
        AlunoAdapter adaptador = new AlunoAdapter(alunos, this);
        lista.setAdapter(adaptador);

    }
}

PDF generator class:

public class PDFActivity extends ListarAlunosActivity{

    Button btnCreate;
    EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layoutactivity_pdf);

        btnCreate = (Button)findViewById(R.id.create);
        editText =(EditText) findViewById(R.id.edittext);
        btnCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createPdf(editText.getText().toString());
            }
        });
    }
    private void createPdf(String sometext){
        // create a new document
        PdfDocument document = new PdfDocument();
        // crate a page description
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600, 1).create();
        // start a page
        PdfDocument.Page page = document.startPage(pageInfo);
        Canvas canvas = page.getCanvas();
        Paint paint = new Paint();
        AlunoAdapter adaptador = new AlunoAdapter(alunos, this);
        lista.setAdapter(adaptador);
        paint.setColor(Color.BLACK);
        canvas.drawText(sometext, 80, 50, paint);
        canvas.drawText(String.valueOf(alunos), 10, 20, paint);
        //canvas.drawText(alunos.toString(), 10, 16, paint);
        //canvas.drawt
        // finish the page
        document.finishPage(page);

        // Create Page 2
        pageInfo = new PdfDocument.PageInfo.Builder(300, 600, 2).create();
        page = document.startPage(pageInfo);
        canvas = page.getCanvas();
        paint = new Paint();
        //paint.setColor(Color.BLUE);
        //canvas.drawCircle(100, 100, 100, paint);
        document.finishPage(page);
        // write the document content
        String directory_path = Environment.getExternalStorageDirectory().getPath() + "/";
        File file = new File(directory_path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String targetPdf = directory_path+"teste495.pdf";
        File filePath = new File(targetPdf);
        try {
            document.writeTo(new FileOutputStream(filePath));
            Toast.makeText(this, "Salvo com sucesso!", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Log.e("main", "error "+e.toString());
            Toast.makeText(this, "Não possível salvar: " + e.toString(),  Toast.LENGTH_LONG).show();
        }
        // close the document
        document.close();
    }
}

class AlunoAdapter:

public class AlunoAdapter extends BaseAdapter {
   private List<Aluno> alunos;
   private Activity atividade;

    public AlunoAdapter(List<Aluno> alunos, Activity atividade){
       this.alunos = alunos;
       this.atividade = atividade;
   }

    @Override
    public int getCount() {
        return alunos.size();
    }

    @Override
    public Object getItem(int position) {
        return alunos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return alunos.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = atividade.getLayoutInflater().inflate(R.layout.item, parent, false);
        TextView id = v.findViewById(R.id.tvId);
        TextView estudante = v.findViewById(R.id.tvEstudante);
        TextView hora = v.findViewById(R.id.tvHora);

        Aluno a = alunos.get(position);
        id.setText(String.valueOf(alunos.get(position).getId()));
        estudante.setText(a.getEstudante());
        hora.setText(a.getHora());
        return v;
    }
}
I hope the PDF will be generated with all existing Listview data.

最佳答案

我们能看到 Aluno 物体吗?您可以尝试更改其中的 toString() 方法。

看起来您正在使用“String.valueOf(alunos)”在 pdf 上打印数据,这就是为什么您只得到一行的原因。

你可以替换

canvas.drawText(String.valueOf(alunos), 10, 20, paint);

for (Aluno a : alunos)
{
   canvas.drawText(a.getId() + "\t" + a.getEstudante() + "\t" + a.getHora()), 10, 20, paint);     
}

现在我认为这可能会在同一行上打印所有 Aluno 对象,因此您可能想尝试类似的操作

int x = 20;
for (Aluno a : alunos)
{
   canvas.drawText(a.getId() + "\t" + a.getEstudante() + "\t" + a.getHora()), 10, x, paint);
   x+=20;     
}

希望对您有所帮助。

关于java - 从 ListView 到 PDF 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57258656/

相关文章:

Java:keyCode、rawCode、primaryLevelUnicode、scancode,它们是什么?

java - 检查列表是否正确

Delphi:存在水平条时手动绘制 ListView

android - 无法解析构造函数 ArrayAdapter

xaml - ListView SelectedItem 与 Xamarin 表单和 ReactiveUI 问题绑定(bind)

java - 如何使用java生成s3风格的访问/ key

java - 如何获取父组件属性

java - 如何解决 "Failed to parse XML in AndroidManifest.xml"?

android - Nv21字节数据在android中保存为彩色H264格式

android:手动处理配置更改的优点/缺点