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>
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 theListViewi.e.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 thetoString()method of the custom type to populate theTextViewin the current row of theListView. 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.