首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在ListView中显示表的内容

无法在ListView中显示表的内容
EN

Stack Overflow用户
提问于 2013-06-25 10:43:14
回答 3查看 531关注 0票数 1

我有一个SQLite数据库,其中包含一个表。我想使用ListActivity和ListView显示表的内容。问题是它只显示了

代码语言:javascript
复制
database.VesproModel@41814440

VesproModel.java类只保存表的字段以及它们的getter和setter。我能够成功地将数据插入到表中,通过从设备中提取数据库并在计算机上查看来验证该表。

这就是我如何处理获取数据的方式:

代码语言:javascript
复制
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中调用这个函数,如下所示:

代码语言:javascript
复制
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的相应布局:

代码语言:javascript
复制
<?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变量的问题:

代码语言:javascript
复制
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打来的:

代码语言:javascript
复制
            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()方法中的无限循环中。

EN

回答 3

Stack Overflow用户

发布于 2013-06-25 10:49:54

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

或者,如果VesproModel类有您想要显示的字段--让我们称之为text --您可以使用ArrayAdapter<String>而不是ArrayAdapter<VesproModel>,然后:

代码语言:javascript
复制
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确切地知道如何显示这些字符串。

如果你需要更多的信息,告诉我,所以我会给出更清楚的解释。

票数 2
EN

Stack Overflow用户

发布于 2013-06-25 11:09:58

您在ResourceId中使用的ArrayAdapter只接受String,因此values对象必须是String类型的Arraylist

现在,您需要创建一个扩展ArrayAdapter<VesproModel>的适配器,然后创建自己的行,以在ArrayAdaptergetView方法中显示注释。

在创建了Adapter之后,可以通过以下方式调用它。

代码语言:javascript
复制
 MyAdapter<VesproModel> adapter = new MyAdapter<VesproModel>(this,
            R.layout.your_layout, values);

其中values = new ArrayList<VesproModel>;

票数 1
EN

Stack Overflow用户

发布于 2013-06-25 11:04:12

莫基托斯的答案是正确的。另一种方法是在自定义类toString()中重写VesproModel方法。然后,它将打印在database.VesproModel@41814440中的任何内容,而不是获取toString()。例如,按照摩斯基托斯的方法,你可以这样做:

代码语言:javascript
复制
@Override
public String toString(){
    return text;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17295218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档