Note that there are some explanatory texts on larger screens.

plurals
  1. POListView does not display with custom adapter class
    text
    copied!<p>I have an <code>Apple</code> class with two fields, <code>colour</code> and <code>rotation</code>, and an <code>AppleView</code> View which draws the apple in that colour and rotation in its <code>onDraw</code> method (this is a simplification of my actual code). The AppleView has a default apple (let's say, red and rotated by 0 degrees) which can be reassigned. Here is some pseudocode for the two classes:</p> <pre><code>public class Apple { int colour; int rotation; } public class AppleView extends View { Apple apple = getDefaultApple(); @Override protected void onDraw(Canvas canvas) { drawApple(canvas, apple); } } </code></pre> <p>I'd like to make a ListView of AppleViews from an ArrayList of apples. I create two layouts: a ListView (<code>R.layout.list_view</code>):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@android:id/list"&gt; &lt;/ListView&gt; </code></pre> <p>and a list item, which is just an <code>AppleView</code> (<code>R.layout.list_item</code>):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;com.mypackage.AppleView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/apple"&gt; &lt;/com.mypackage.AppleView&gt; </code></pre> <p>Finally, I extend <code>ListActivity</code> and use a custom adapter class to draw the list items:</p> <pre><code>public class MyListActivity extends ListActivity { ArrayList&lt;Apple&gt; apples = new ArrayList&lt;Apple&gt;(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_view); // Add 100 random apples to the list for demonstration. for (int i = 0; i &lt; 100; i++) { apples.add(getRandomApple()); } getListView().setAdapter(new MyAdapter(this, R.layout.list_item, apples)); } class MyAdapter extends ArrayAdapter&lt;Apple&gt; { public MyAdapter(Context context, int resourceID, ArrayList&lt;Apple&gt; items) { super(context, resourceID, items); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.list_item, parent, false); } // Change this AppleView's apple to the new ArrayList element at this position. AppleView appleView = (AppleView) convertView.findViewById(R.id.apple); appleView.apple = apples.get(position); return convertView; } } } </code></pre> <p>Although all the right methods are being called (i.e. getView and onDraw for each apple), the ListView does not display:</p> <p><img src="https://i.stack.imgur.com/2j6NP.png" alt="enter image description here"></p> <p>Where is the error in the above code? </p>
 

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