从列表视图到PDF的数据 [英] Data from a listview to a PDF

查看:77
本文介绍了从列表视图到PDF的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用列表视图中的所有数据生成PDF.我正在使用包含以下列的自定义列表视图:id,estudante,hora.
我只能将estudante列数据写入PDF,但不能返回id列和hora列. 我想以一种有条理的方式列出它,但我只能将它放在一行上.

I want to generate a PDF with all the data in a listview. I am using a custom listview that contains the columns: id, estudante, hora.
I can only write the estudante column data to the PDF, but I can't return the id column and the hora column. I would like to list it in an organized manner, but I am only getting it on a single line.

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()方法.

Can we see the object Aluno? You could try changing your toString() method in it.

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

It looks you are printing data on the pdf using "String.valueOf(alunos)", which is why you get only one single line.

您可以替换

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对象,因此您可能要尝试类似的操作

Now I think this may print all Aluno objects on the same line, so you may want to try something like

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

希望对您有帮助.

这篇关于从列表视图到PDF的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆