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:
Main.java
public class Main extends Activity
{
private ListView listView;
private List<Row> listRows;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.year);
//Added initialisation from answers
listRows = new ArrayList<Row>();
//Add some generic rows to the list for test purposes
for (int i=0; i<4; i++) {
listRows.add(new Row(this));
}
listView = (ListView)findViewById(R.id.ListView1);
listView.setAdapter(new RowAdapter(this, listRows));
}
}
RowAdapter.java
public class RowAdapter extends BaseAdapter
{
private List<Row> elements;
private Context context;
public RowAdapter(Context c, List<Row> 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);
}
}
Row.java
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);
}
}
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.
Thanks for your help in advance.
EDIT: The ListView now loads but shows no elements. I’m using the main.xml file shown below.
main.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:id="@+id/ListView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
You need
listRows = new ArrayList<Row>();to initialize the list before adding elements to it.In general, try examining the output of logcat in your Eclipse debug perspective or in ddms. In this case it would’ve said something to the effect of “null pointer in onCreate” and you would’ve saved yourself some time.
Edit I suspect this is because your Row views don’t know how big they should be. You might try overriding
onMeasurewith a simple method as a sanity check:The need to do this can be mitigated by some better factoring of your code. The underlying data of an Adapter isn’t meant to touch views directly; generally the views are created in
getView.For instance, you might have in
res/layout/row.xml:This way you can set xml properties on your custom view, which is nice if you use it in multiple places.
Then HelloCanvas would define the
HelloCanvas(Context, AttributeSet)constructor, which the layout inflater utilizes:And finally your
getViewmethod would inflaterow.xml:This way you aren’t generating new views if you don’t have to, and the underlying data type of your ArrayAdapter (eliminates some of your excess code) can be String.