0

Im following a tutorial and i have created a database class and a activity class. Here is my activity class:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    datasource = new CommentsDataSource(this);
    datasource.open();

    List<Comment> values = datasource.getAllComments();

    // Use the SimpleCursorAdapter to show the
    // elements in a ListView
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

My db is a bit different but most of the stuff is the same as in tutorial. Comment is just a setter/getter class. Now the problem is that in my list i want to display comment name but i get "com.example.blabla.Comment@40dca9d0". I think it is because i am passing the whole comment class to the adapter. How would be the right way to pass the name?

Here is the link to tutorial, i must be missing something because it seems to work there but i dont know what exactly: http://www.vogella.com/articles/AndroidSQLite/article.html#sqliteoverview_sqliteopenhelper

1 Answer 1

1
  // Will be used by the ArrayAdapter in the ListView
  @Override
  public String toString() {
    return comment;
  }

Did you make sure you added this to your Comment class?
In java the default implementation of toString() is Class@Hashcode which is what currently yours is showing, hence you need to override the default implementation by returning the comment.

You do not see toString() being called because it says in DOCS(parag2)

However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it works now :) ... but do you also know where/how/why the "toString" method is called in this case? I cant see it in the code so it must be called automaticly somewhere?
Updated to answer your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.