gpt4 book ai didi

java - 从 ListView 到 PDF 的数据

转载 作者:行者123 更新时间:2023-12-02 02:24:56 25 4
gpt4 key购买 nike

我想生成一个包含 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com