我有一个包含表的 SQLite 数据库。我想使用 ListActivity 和 ListView 显示表格的内容。问题是它只显示这个
database.VesproModel@41814440
VesproModel.java 类仅包含表的字段以及它们的 getter 和 setter。通过从设备中提取数据库并在计算机上查看它,我能够成功地将数据插入到我已验证的表中。
这就是我处理获取数据的方式:
public List<VesproModel> getAllComments() {
List<VesproModel> comments = new ArrayList<VesproModel>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_TPCS_VESSEL, allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
VesproModel comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
cursor.close();
return comments;
}
private VesproModel cursorToComment(Cursor cursor) {
VesproModel comment = new VesproModel();
Log.d("cursorToComment", "Before if cursorToComment");
if( cursor != null && cursor.moveToFirst() ){
Log.d("cursorToComment", "inside cursorToComment");
comment.setId(cursor.getLong(0));
comment.setImo_number(cursor.getString(1));
...
...
...}
return comment;
}
我在 ListActivity 中调用这个函数,如下所示:
public class CurrentList extends ListActivity {
private VesproDataSource datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currentlist);
datasource = new VesproDataSource(this);
datasource.open();
List<VesproModel> values = datasource.getAllComments();
ArrayAdapter<VesproModel> adapter = new ArrayAdapter<VesproModel>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
}
这是上面 ListActivity 的对应布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
如何在屏幕上显示表格中的数据。请建议我应该使用哪种类型的布局,因为表格中有很多列。谢谢。
编辑 1:
正如建议的那样,我创建了一个自定义类来扩展 ArrayAdapter 类来处理显示 Vespromodel 变量:
public class ItemAdapter extends ArrayAdapter<VesproModel> {
private List<VesproModel> objects;
public ItemAdapter(Context context, int textViewResourceId, List<VesproModel> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
if (i != null) {
// I inserted some textviews to test the data out. For the final display , I would need many more textviews or a TableLayout
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
TextView mt = (TextView) v.findViewById(R.id.middletext);
TextView mtd = (TextView) v.findViewById(R.id.middletextdata);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
TextView btd = (TextView) v.findViewById(R.id.desctext);
if (tt != null){
tt.setText("Imo_number: ");
}
if (ttd != null){
ttd.setText(i.getImo_number());
}
if (mt != null){
mt.setText("Vessel Name : ");
}
if (mtd != null){
mtd.setText(i.getVessel_name());
}
if (bt != null){
bt.setText("Vessel_type : ");
}
if (btd != null){
btd.setText(i.getVessel_type());
}
}
return v;
}
}
我从 ListActivity 中调用它,如下所示:
List<VesproModel> values = dsrc.getAllComments(); // function is defined in the above section.
m_adapter = new ItemAdapter(this, R.layout.list_item, values);
setListAdapter(m_adapter);
现在活动陷入了 getAllComments() 方法内的无限循环。