【问题标题】:Cannot display contents of table in ListView无法在 ListView 中显示表格内容
【发布时间】:2013-06-22 03:00:20
【问题描述】:

我有一个包含表的 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);

现在 Activity 在 getAllComments() 方法中陷入无限循环。

【问题讨论】:

    标签: android listview android-sqlite tablelayout listactivity


    【解决方案1】:

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

    或者,如果VesproModel 类有例如你想要显示的字段——我们称之为text——你可以使用ArrayAdapter&lt;String&gt; 而不是ArrayAdapter&lt;VesproModel&gt;,然后:

    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】:

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

      现在,您需要创建一个扩展ArrayAdapter&lt;VesproModel&gt; 的Adapter,然后在ArrayAdaptergetView 方法中创建自己的行来显示cmets。

      创建适配器后, 你可以说它。

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

      在哪里values = new ArrayList&lt;VesproModel&gt;;

      【讨论】:

        【解决方案3】:

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

        @Override
        public String toString(){
            return text;
        }
        

        【讨论】:

        • 不太明白。你说的这个“蚊子”是谁?
        • 我的意思是 Piotr Chojnacki。大声笑:P
        • 覆盖 VesproModel 类中的 toString() 方法。你在 toString() 方法中输入的任何内容都将写入你的适配器,而无需创建自定义适配器。
        • 是的,我试图覆盖 toString() 方法。但问题是我在 VesproModel 类中混合了字符串和整数变量。如何覆盖 toString 方法来合并它?
        • 字符串混合?我猜你有一种方法可以在你的类中检索这些字符串。您将使用什么方法来获取这些字符串?
        猜你喜欢
        • 1970-01-01
        • 2013-02-07
        • 1970-01-01
        • 2015-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-23
        • 1970-01-01
        相关资源
        最近更新 更多