Question

I have a POJO like this:

public class Color {
 private int id;
 private int name;
 //getter setter
}

In my AsyncTask I call the server and get bunch of these objects and then want to populate my list. I do this by:

    @Override
    protected String doInBackground(String... strings) {
        colorsList = MyManager.INSTANCE.getService().getColors();
        return null;
    }

    @Override
    protected void onPostExecute(String file_url) {
        setListAdapter(new ArrayAdapter<Color>(MyActivity.this,R.layout.list_black_text,R.id.name, colorsList));
    }

list_black_text looks like below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- hidden color id-->
    <TextView
    android:id="@+id/color_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

    <!--color name-->
    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16dip"
        android:textColor="#000000"
        android:paddingTop="15dip"
        android:paddingBottom="15dip"
        android:paddingLeft="10dip"
        android:textStyle="bold"/>
</RelativeLayout>

Question

When I run the app the list seems to be populated by objects so I see it being populated with stuff like

com.my.package.Color@2343fa0
com.my.package.Color@2343fa0
...

What do I need to change so that I can see the name of the color in the list?

Note: I don't want to create a separate string array. I have tried that and it works. But I have additional requirement of being able to detect which ID color was clicked when the user clicks on a color name.

Was it helpful?

Solution

The list is using the passed object's toString method for display. So, if you want to display color names, just add a name variable, override the toString method and return the name of the color.

public class Color {
    private int id;
    private int name;
    private String strName;
    public String toString(){return this.strName;}
    //getter setter
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top