1

我有一个包含表的 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() 方法内的无限循环。

4

3 回答 3

2

ListView不知道如何显示来自 的值VesproModel,这就是它显示VesproModel类名的原因。你想要的是实现你自己的适配器,它知道如何从你的类的对象中获取数据。

或者,如果VesproModel类有例如你想要显示的字段——我们称之为text——你可以使用ArrayAdapter<String>代替ArrayAdapter<VesproModel>,然后:

List<VesproModel> values = datasource.getAllComments();
ArrayList<String> valuesToDisplay = new ArrayList<String>();
for(VesproModel vm : values) {
    valuesToDisplay.add(vm.text);
}

ArrayAdapter<String> adapter = new ArrayAdapter<VesproModel>(this,
        android.R.layout.simple_list_item_1, valuesToDisplay);
setListAdapter(adapter);

这样你就有了字符串列表,并且ArrayAdapter确切地知道如何显示这些字符串。

如果您需要更多信息,请告诉我,以便我给出更清晰的解释。

于 2013-06-25T10:49:54.873 回答
1

ResourceId您在仅接受字符串中使用,ArrayAdapter因此值对象必须是仅字符串Arraylist类型。

现在,您需要创建一个扩展的 Adapter,ArrayAdapter<VesproModel>然后创建自己的行来getView显示ArrayAdapter.

创建 Adapter 后,可以通过说调用它。

 MyAdapter<VesproModel> adapter = new MyAdapter<VesproModel>(this,
            R.layout.your_layout, values);

在哪里values = new ArrayList<VesproModel>;

于 2013-06-25T11:09:58.653 回答
0

莫基托斯的回答是正确的。另一种方法是toString()在您的自定义类中覆盖方法VesproModel。然后,database.VesproModel@41814440它不会打印您放入的任何内容,而不是打印toString()。例如,按照 Mosquitos 方法,您可以执行以下操作:

@Override
public String toString(){
    return text;
}
于 2013-06-25T11:04:12.097 回答