Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot display contents of table in ListView
    primarykey
    data
    text
    <p>I have an SQLite database which contains a table. I want to display the contents of the table using ListActivity and ListView. The problem is it only displays this </p> <pre><code>database.VesproModel@41814440 </code></pre> <p>The VesproModel.java Class just holds the fields of the table along with their getters and setters. I am able to successfully insert the data into the table which I have verified by pulling the database from the device and viewing it on the computer.</p> <p>This is how I am dealing with fetching the data : </p> <pre><code>public List&lt;VesproModel&gt; getAllComments() { List&lt;VesproModel&gt; comments = new ArrayList&lt;VesproModel&gt;(); 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 &amp;&amp; cursor.moveToFirst() ){ Log.d("cursorToComment", "inside cursorToComment"); comment.setId(cursor.getLong(0)); comment.setImo_number(cursor.getString(1)); ... ... ...} return comment; } </code></pre> <p>And I am calling this function in a ListActivity like so :</p> <pre><code>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&lt;VesproModel&gt; values = datasource.getAllComments(); ArrayAdapter&lt;VesproModel&gt; adapter = new ArrayAdapter&lt;VesproModel&gt;(this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } } </code></pre> <p>And this is the corresponding layout for the above ListActivity : </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@android:id/list" android:layout_width="wrap_content" android:layout_height="wrap_content" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>How can I display the data in the table on the screen. Please suggest what type of layout I should use as there are quite a few columns in the table. Thanks.</p> <p><strong>EDIT 1 :</strong></p> <p>As suggested , I made a custom class extending the ArrayAdapter class to handle displaying Vespromodel variables:</p> <pre><code>public class ItemAdapter extends ArrayAdapter&lt;VesproModel&gt; { private List&lt;VesproModel&gt; objects; public ItemAdapter(Context context, int textViewResourceId, List&lt;VesproModel&gt; 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; } </code></pre> <p>}</p> <p>I am calling this from the ListActivity like so :</p> <pre><code> List&lt;VesproModel&gt; values = dsrc.getAllComments(); // function is defined in the above section. m_adapter = new ItemAdapter(this, R.layout.list_item, values); setListAdapter(m_adapter); </code></pre> <p>Now the activity is getting stuck in an infinite loop inside the getAllComments() method.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload