【问题标题】:Data from a listview to a PDF从列表视图到 PDF 的数据
【发布时间】:2019-08-31 20:26:38
【问题描述】:

我想生成一个包含列表视图中所有数据的 PDF。我正在使用包含以下列的自定义列表视图: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.

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    我们能看到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;     
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      • 2013-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多