在listview中显示记录

时间:2014-10-25 06:36:08

标签: android sqlite listview

我必须从数据库中获取所有记录并在tableLayout中显示它们。我做了第一部分。我创建了一个从数据库中获取记录并将其存储在List中的方法。现在我的问题是如何将记录从List放到表格布局? 这是我的方法:

public List<Hospital> getallrecords(){
     List<Hospital> info = new LinkedList<Hospital>();
     String query = "SELECT * FROM " + "PATIENT_INFO";
        Cursor cursor = db.rawQuery(query, null);
        Hospital hospital = null;
        if(cursor.moveToFirst()){
            do{
            hospital = new Hospital();
            hospital.setMRNo(cursor.getString(1));
            hospital.setTitle(cursor.getString(2));
            hospital.setName(cursor.getString(3));
            hospital.setPrename(cursor.getString(4));
            hospital.setPredecessorname(cursor.getString(5));
            hospital.setPhone1(Integer.parseInt(cursor.getString(6)));
            hospital.setPhone2(Integer.parseInt(cursor.getString(7)));
            hospital.setAddress(cursor.getString(8));

            info.add(hospital);
            }while(cursor.moveToNext());
        }
    return info;

}

1 个答案:

答案 0 :(得分:0)

您可以使用以下方式获取表格布局中的数据。

TableLayout tableLayout = (TableLayout) findViewById(R.id.layoutId);

for(i=0;i<info.size();i++){
TableRow row = new TableRow(this); row .setId(i); row .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); TextView tile= new TextView(this); title.setId(200+i); tile.setText(info.getTitle()); title.setPadding(2, 0, 5, 0); title.setTextColor(Color.WHITE); row.addView(title); TextView name= new TextView(this); name.setId(200+i); name.setText(info.getName()); name.setTextColor(Color.WHITE); row.addView(name); //same as you can add as many column as you want. tablelayout.addView(row, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); }

或者你可以使用ListView(因为它是一种更好的显示列表数据的方式):
请遵循以下教程:
http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter