ListView ArrayAdapter未显示正确的数据

时间:2015-09-17 21:49:45

标签: java android sqlite

我让它工作,以便它显示从listview中的数据库中检索到的一些数据。它显示了我的数据库中的10行,只有10行,但问题是,它显示为static byte[] Serialize(object o) { if (o == null) { return null; } BinaryFormatter binaryFormatter = new BinaryFormatter(); using (MemoryStream memoryStream = new MemoryStream()) { binaryFormatter.Serialize(memoryStream, o); byte[] objectDataAsStream = memoryStream.ToArray(); return objectDataAsStream; } } 。为什么会这样,我该如何解决这个问题呢?所以问题出在LocationHistory.java活动中。它在我的显示器上显示奇怪的包名称。

activity_location_history.xml:

com.example.somename.LocationData@23157b39

LocationsDataSource.java:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".LocationHistory">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

LocationHistory.java:

    // some code
    private static final String[] allColumns = {
                LocationsDBOpenHelper.COLUMN_ID,
                LocationsDBOpenHelper.COLUMN_LATITUDE,
                LocationsDBOpenHelper.COLUMN_LONGITUDE,
                LocationsDBOpenHelper.COLUMN_TIME};
    // some code

public List<LocationData> findAll() {
        List<LocationData> locations = new ArrayList<>();

        Cursor cursor = database.query(LocationsDBOpenHelper.TABLE_LOCATIONS, allColumns, null, null, null, null, null);

        Log.i(LOGTAG, "Returned " + cursor.getCount() + " rows");
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                LocationData locationdata = new LocationData();
                locationdata.setId(cursor.getLong(cursor.getColumnIndex(LocationsDBOpenHelper.COLUMN_ID)));
                Log.i(LOGTAG, "COLUMN_ID " + locationdata.getId());
                locationdata.setLatitude(cursor.getString(cursor.getColumnIndex(LocationsDBOpenHelper.COLUMN_LATITUDE)));
                Log.i(LOGTAG, "COLUMN_LATITUDE " + locationdata.getLatitude());
                locationdata.setLongitude(cursor.getString(cursor.getColumnIndex(LocationsDBOpenHelper.COLUMN_LONGITUDE)));
                Log.i(LOGTAG, "COLUMN_LONGITUDE " + locationdata.getLongitude());
                locationdata.setTime(cursor.getString(cursor.getColumnIndex(LocationsDBOpenHelper.COLUMN_TIME)));
                Log.i(LOGTAG, "COLUMN_TIME " + locationdata.getTime());
                locations.add(locationdata);
                Log.i(LOGTAG, "locationdata " + locationdata.toString());
                Log.i(LOGTAG, "locations " + locations.toString());
            }
        }
        return locations;
    }

LocationData.java:

// some code

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_history);

        datasource = new LocationsDataSource(this);
        datasource.open();

        List<LocationData> locationdata = datasource.findAll();
        if (locationdata.size() == 0) {
            locationdata = datasource.findAll();
        }

        ArrayAdapter<LocationData> adapter = new ArrayAdapter<LocationData>(this,
                android.R.layout.simple_list_item_1, locationdata);
        setListAdapter(adapter);
    }

// some code

logcat的:

public class LocationData {
    private long id;
    private String latitude;
    private String longitude;
    private String time;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

2 个答案:

答案 0 :(得分:0)

我认为问题出在这里

  

Log.i(LOGTAG,“locationdata”+ locationdata)

实际上称为Log.i(LOGTAG, "locationdata " + locationdata.toString())。但是LocationData.toString()方法没有实现并使用了Object.toString()

答案 1 :(得分:0)

致电时

asarray()

您实际上是在尝试将List locationdata列表转换为String,您可以为原始类型执行此操作,但如果您想在类中执行此操作,则必须提供自己的toString实现。

请添加

 Log.i(LOGTAG, "locationdata " + locationdata);

再试一次。

祝你好运