Question

I cant get my data to display in ListView. I have removed all unnecessary code to keep this simple. The program works fine, but the only data that is displayed is "Model.Locations @ 421ec04eo" Why is this happening?

PinPoint Class

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.locations);

        ListView listView1 = (ListView) findViewById(R.id.listView1);

        ArrayList<Locations> items = new ArrayList<Locations>();
        Locations sr1 = new Locations();
        for (int i = 0; i < 15; i++) 
        {
            sr1.setAdress("Blablabla");
            sr1.setTitle("Home");
            sr1.setDistance("200m");
            items.add(sr1);
        }

        ArrayAdapter<Locations> adapter = new ArrayAdapter<Locations>(this,
                android.R.layout.simple_list_item_multiple_choice, items);

        listView1.setAdapter(adapter);
    }
}

Location class

    package Model;


    public class Locations 
    {
    int midlat;
    int midlong;
    String title;
    String adress;
    String distance;

    public int getMidlat() {
        return midlat;
    }


    public String getDistance() {
        return distance;
    }


    public void setDistance(String distance) {
        this.distance = distance;
    }


    public void setMidlat(int i) {
        this.midlat = i;
    }


    public int getMidlong() {
        return midlong;
    }


    public void setMidlong(int midlong) {
        this.midlong = midlong;
    }


    public String getTitle() {
        return title;
    }


    public void setTitle(String title) {
        this.title = title;
    }


    public String getAdress() {
        return adress;
    }


    public void setAdress(String adress) {
        this.adress = adress;
    }


    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return super.toString();
    }
}

locations XML.FILE

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:text="Button" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

    <TextView
        android:id="@+id/toptext"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center_vertical" />

    <TextView
        android:id="@+id/bottomtext"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:ellipsize="marquee"
        android:singleLine="true" />

</RelativeLayout>
Was it helpful?

Solution

You need to override the toString() method of your custom type, which you started to do, but then just called the super which negates the purpose of overriding it in the first place.

Make the toString() return what you want the arrayadapter to display in the ListView i.e.

@Override
public String toString(){
  return title + address + distance;
}

Of course, this is just an example, you can implement it to fit your needs.

Also, the reason is because ArrayAdapter<T> by default calls the toString() method of the custom type to populate the TextView in the current row of the ListView. If you need the list to show something special above what you can put together in the overrided toString method you will have to create a custom adapter.

OTHER TIPS

You are passing an array of memory addresses to the adapter, so it is expected.

ArrayAdapter<Locations> adapter = new ArrayAdapter<Locations>(this,
                android.R.layout.simple_list_item_multiple_choice, items);

If you want to display the address, title and distance in your views, please consider a custom listview.

The basic ArrayAdapter just uses the Map.Location.toString() to populate the ListView's item views. With a basic ArrayAdapter the default item view is a TextView. In order to properly display your location you can override the toString() function to what you want it to show for a Map.Location instance:

@Override
public String toString(){
*"String that you want"*
}

Or another way would to be create a custom BaseAdapter and override the getView() method of your custom BaseAdapter to show a specialize layout.xml. Look here for instructions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top