I am trying to populate a list view with a select all query from a data base (1 Primary Key, and 4 attributes. what I have now is not throwing errors, but its not generating anything that is usable. Below is the query that I am using:
public List<SavedLocation> getAllLocationDescriptions() {
List<SavedLocation> locationList = new ArrayList<SavedLocation>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_LOCATIONS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
SavedLocation location = new SavedLocation();
location.setLocation(cursor.getString(0));
// Adding locationt to list
locationList.add(location);
} while (cursor.moveToNext());
}
return locationList;
}
This is my list view activity. (note: the log output is writing the correct results, so that seems to be working)
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class SavedLocationsListTest extends ListActivity{
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent intent = getIntent();
DatabaseHandler db = new DatabaseHandler(this);
List<SavedLocation> test = db.getAllLocationDescriptions();
for (SavedLocation cn : test){
String log = "ID: "+ cn.getID()+ ", Location:" + cn.getLocation() + ", Accuracy:"+ cn._accuracy+ ", Description: " +cn._description+ ", Provider:" + cn.getProvider();
Log.d("Location: ", log);}
setListAdapter (new ArrayAdapter<SavedLocation>(this, R.layout.list_test,test));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
}
}
and this is my xml layout
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
and now as I said, I'm not getting errors, but I feel like what I am getting is even more frustrating; I have 4 records in my database and this is what I keep getting as the output when running the app:
it looks like I'm able to populate the list view, I'm just doing it wrong.
The reason your TextViews are displaying information like com.example.gpstest1.SavedLocation@41eb8128
is that you haven't overriden the toString()
method in your SavedLocation
class.
The ArrayAdapter class has no idea how to convert your Java objects into a readable format. It can't read your mind and determine what a valid textual representation of a SavedLocation
might be. The best it can do is call toString()
on your object and hope for the best.
According to the Object.toString()
documentation, the default implementation of toString()
does this:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
You have a few options. The easiest is to just override toString()
in your SavedLocation
and have it output what you want. This will work well if you really just want a single TextView to represent each entry.
If you want a more complicated layout, you will want to create your own ArrayAdapter subclass that overrides getView()
to generate the appropriate View for each row in the ListView.