vendredi 6 mai 2016

Populate listview from SQLite returns memory address

I am trying to populate listview from my SQLite db using Java in AndroidStudio 2.1

The problem I have is that the data being returned is a memory address and not the object as shown below

http://ift.tt/1SUDJN3

Here is my MainActivity class:

public class MainActivity extends ListActivity {

DBHelper db;

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

    db = new DBHelper(this, null, null, 1);

    db.addTrack(new Tracks("track1", "www.abc.com"));
    db.addTrack(new Tracks("track2", "www.def.com"));

    List<Tracks> list = db.getAllTracks();

    ArrayAdapter<Tracks> adapter = new ArrayAdapter<Tracks>(
            this,
            android.R.layout.simple_list_item_1,
            list);

    setListAdapter(adapter);

}

}

Here is the method for getAllTracks()

public List<Tracks> getAllTracks(){
    List<Tracks> tracks = new LinkedList<Tracks>();

    String query = "SELECT * FROM " + TABLE_TRACKS;

    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.rawQuery(query,null);

    Tracks track = null;
    if(cursor.moveToFirst()){
        do{
            track = new Tracks();
            track.set_id(Integer.parseInt(cursor.getString(0)));
            track.set_trackName(cursor.getString(1));
            track.set_trackURL(cursor.getString(2));

            tracks.add(track);
        }while(cursor.moveToNext());
    }

    return tracks;
}

What am I doing wrong?

I would like the names track1 and track2 displayed in a listview and then after user clicks on the listview item it loads the associated URL.

Please help

Thank you very much

Aucun commentaire:

Enregistrer un commentaire