0

I am trying to display data in a listview from my sqlite table. I can view the table and it seems as if the information is going in correct. When I run my app the listview comes up but it displays my package name with some numbers and letters instead of the data from my table. Any help would be appreciated. Thanks.

Here is my code to get all the information from the table

public List<Habits> getAllHabits(){
    SQLiteDatabase db = this.getWritableDatabase();
    List<Habits> habitsList = new ArrayList<Habits>();
    String query = "SELECT * FROM " + TABLE_HABIT;
    Cursor cursor = db.rawQuery(query,null);
    if(cursor.moveToFirst()){
        do{
            Habits habit = new Habits();
            habit.setHabit(cursor.getString(1));
            habit.setDate(cursor.getString(2));
            habitsList.add(habit);
        }while(cursor.moveToNext());

    }
    cursor.close();
    return habitsList;

}

Code to display it:

public class ViewHabits extends ListActivity {

Cursor cursor;
private Databasehandler db;
String info;
private ListView datalist;
ArrayList<String> databaseList = new ArrayList<String>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db = new Databasehandler(this);

    List<Habits> values = db.getAllHabits();
    ArrayAdapter<Habits> adapter = new ArrayAdapter<Habits>(this,android.R.layout.simple_list_item_1,values);

    setListAdapter(adapter);

}

1 Answer 1

1

The simplest way is to override toString() method of Habit class:

However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list.

From docs: ArrayAdapter

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.