0

I have a ParseQueryAdapter class, and my items are being retrieved successfully. However, if I have this for loop to view the objects (strings):

 for (int i =0;i<adapter.getCount();i++){
        Log.d("Ab", adapter.getItem(i).toString());
    }

I get these:

   com.example.ab_me.example.classname@41ef11f8    
   com.example.ab_me.example.classname@41ef33d8
   com.example.ab_me.example.classname@41ef40c0

Note: For security reasons, I replaced my app name with example and my classname with classname. Why is this? It is supposed to be:

listItem1
listItem2
listItem3

Thanks in advance

Manuel
  • 14,274
  • 6
  • 57
  • 130
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

1 Answers1

0

The Object that is returned from getItem method call does not override toString() method, so java uses a default implementation in this case because it doesn't 'know' how to convert the object to a String type.

If you own this type, simply @Override the toString() method with your own implementation.

Example:

Assuming your type name is YourType. I mean, adapter.getItem(i) returns an object of YourType. In YourType.java:

class YourType {
    private String exampleField1;
    private String exampleField2;

    @Override
    public String toString() {
        return exampleField1 + ", " + exampleField2;
    }
}
Gennadii Saprykin
  • 4,505
  • 8
  • 31
  • 41
  • Give an example please? Sorry I'm new – Ali Bdeir Mar 02 '16 at 15:48
  • Wait nevermind my deleted comments. Ok. what is exampleField1, how do I define it? – Ali Bdeir Mar 02 '16 at 16:03
  • In your case the type is `com.example.ab_me.example.classname`. I have no idea what fields you have in this class.. Just override a toString method in your classname and provide your own implementation to let java know how to covert your classname object to String. In my case I have 2 example field just to show how the implementation might look like. You don't have to define those. – Gennadii Saprykin Mar 02 '16 at 16:14