Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4083264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:17:27+00:00 2026-05-20T18:17:27+00:00

I’m attempting to draw to a Canvas inside each element of a ListView. Currently

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:17:28+00:00Added an answer on May 20, 2026 at 6:17 pm

    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 onMeasure with a simple method as a sanity check:

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       setMeasuredDimension(100, 100);
    }
    

    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:

    <?xml version="1.0" encoding="utf-8"?>
    <com.example.HelloCanvas
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        ... other attributes like background color ...
        />
    

    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:

    public HelloCanvas(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    

    And finally your getView method would inflate row.xml:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HelloCanvas hello = (HelloCanvas) convertView;
        if (hello == null) {
            // inflate a new view
            hello = (HelloCanvas) 
              LayoutInflater.from(getContext()).inflate(R.layout.row, parent, false);
        }
        hello.setHelloString(getItem(position));
        return hello;
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to select an H1 element which is the second-child in its group
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.