我想用列表视图中的所有数据生成一个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.
最佳答案
我们可以看到物体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;
}
希望对您有所帮助。