简体   繁体   English

Android:无法获取要在ListView中显示的数据

[英]Android: Cant get data to display in ListView

I cant get my data to display in ListView. 我无法将数据显示在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? 该程序可以正常运行,但是显示的唯一数据是“ Model.Locations @ 421ec04eo”为什么会发生这种情况?

PinPoint Class PinPoint类

    @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. 您需要重写自定义类型的toString()方法,这是您开始要做的,但是您只需调用super即可,它首先否定了覆盖它的目的。

Make the toString() return what you want the arrayadapter to display in the ListView ie 使toString()返回您希望arrayadapter在ListView显示的内容,即

@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 . 另外,原因是因为默认情况下, ArrayAdapter<T>调用自定义类型的toString()方法以在ListView的当前行中填充TextView 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. 如果您需要列表在可以覆盖的toString方法中放在一起的上方显示特殊的内容,则必须创建一个自定义适配器。

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. 基本的ArrayAdapter仅使用Map.Location.toString()填充ListView的项目视图。 With a basic ArrayAdapter the default item view is a TextView. 使用基本的ArrayAdapter时,默认的项目视图是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: 为了正确显示您的位置,您可以将toString()函数覆盖为您希望其在Map.Location实例中显示的内容:

@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. 否则,将创建自定义BaseAdapter并覆盖自定义BaseAdapter的getView()方法以显示特殊的layout.xml。 Look here for instructions. 在这里查看说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM