Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing to a Canvas inside a ListView element
    primarykey
    data
    text
    <p>I'm attempting to draw to a Canvas inside each element of a ListView. Currently I'm using the following code, which gives a Force Close as soon as I launch the activity:</p> <p><b>Main.java</b></p> <pre><code>public class Main extends Activity { private ListView listView; private List&lt;Row&gt; listRows; public void onCreate(Bundle bundle) { super.onCreate(bundle); setContentView(R.layout.year); //Added initialisation from answers listRows = new ArrayList&lt;Row&gt;(); //Add some generic rows to the list for test purposes for (int i=0; i&lt;4; i++) { listRows.add(new Row(this)); } listView = (ListView)findViewById(R.id.ListView1); listView.setAdapter(new RowAdapter(this, listRows)); } } </code></pre> <p><br> <b>RowAdapter.java</b></p> <pre><code>public class RowAdapter extends BaseAdapter { private List&lt;Row&gt; elements; private Context context; public RowAdapter(Context c, List&lt;Row&gt; rows) { this.elements = rows; this.context = c; } @Override public int getCount() { return elements.size(); } @Override public Object getItem(int position) { return elements.get(position); } @Override public long getItemId(int id) { return id; } @Override public View getView(int position, View convertView, ViewGroup parent) { return elements.get(position); } } </code></pre> <p><br> <b>Row.java</b></p> <pre><code>public class Row extends View { public Row(Context context) { super(context); } @Override public void onDraw(Canvas canvas) { //Draw simple string as an example Paint paint = new Paint(); paint.setColor(Color.WHITE); canvas.drawText("Text here...", 0, 0, paint); } } </code></pre> <p>I realise there are probably many problems with my approach to this, so if anyone could point me in the right direction I'd be grateful.</p> <p>Thanks for your help in advance.</p> <p><br> <b>EDIT:</b> The ListView now loads but shows no elements. I'm using the main.xml file shown below.</p> <p><b>main.xml</b></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"&gt; &lt;ListView android:id="@+id/ListView1" android:layout_width="wrap_content" android:layout_height="wrap_content"&gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    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