0

I am trying to get string values from Db like below:

List<Workout> workouts = AppDatabase.getInstance(getApplicationContext()).getWorkoutDao().getAll();

txtShowWorkout.setText(workouts.get(0).toString());

However, it going to give output like that: I would like to get string value but it is writing pointer values.

com.example.fitnessapp.context.Workout@78db232 com.example.fitnessapp.context.Workout@d2b4056

Also my getAll() function is below:

 @Override
  public List<Workout> getAll() {
    final String _sql = "SELECT * FROM Workout";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    __db.assertNotSuspendingTransaction();
    final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
    try {
      final int _cursorIndexOfİd = CursorUtil.getColumnIndexOrThrow(_cursor, "id");
      final int _cursorIndexOfWorkoutName = CursorUtil.getColumnIndexOrThrow(_cursor, "workout_name");
      final int _cursorIndexOfFocusArea = CursorUtil.getColumnIndexOrThrow(_cursor, "focus_area");
      final int _cursorIndexOfEquipment = CursorUtil.getColumnIndexOrThrow(_cursor, "Equipment");
      final int _cursorIndexOfReps = CursorUtil.getColumnIndexOrThrow(_cursor, "reps");
      final int _cursorIndexOfDate = CursorUtil.getColumnIndexOrThrow(_cursor, "date");
      final List<Workout> _result = new ArrayList<Workout>(_cursor.getCount());
      while(_cursor.moveToNext()) {
        final Workout _item;
        _item = new Workout();
        _item.id = (int) _cursor.getInt(_cursorIndexOfİd);
        _item.workout_name = _cursor.getString(_cursorIndexOfWorkoutName);
        _item.focus_area = _cursor.getString(_cursorIndexOfFocusArea);
        _item.Equipment = _cursor.getString(_cursorIndexOfEquipment);
        _item.reps = _cursor.getString(_cursorIndexOfReps);
        _item.date = _cursor.getString(_cursorIndexOfDate);
        _result.add(_item);
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }
Mehmet
  • 65
  • 1
  • 7
  • You print `List` which is returned by getAll() instead of a String – Zain Dec 13 '21 at 23:09
  • I don't quite understand, can you give some more details? thanks @Zain – Mehmet Dec 13 '21 at 23:14
  • `workouts` object is `List` and hence `workouts.get(0)` is a `Workout` not a String.. so you need a way to convert it to a String.. Can you share `Workout` class? – Zain Dec 13 '21 at 23:20
  • You can override `toString()` within the `Workout` class and adjust the returned value to what you want to show in `txtShowWorkout.setText(workouts.get(0).toString())` – Zain Dec 13 '21 at 23:33
  • Workout class, please check it. @Zain ``` @Entity public class Workout { @PrimaryKey(autoGenerate = true) public int id; @ColumnInfo public String workout_name; @ColumnInfo public String focus_area; @ColumnInfo public String Equipment; @ColumnInfo public String reps; @ColumnInfo public String date; } ``` – Mehmet Dec 14 '21 at 00:39
  • I think you need the workout_name, so txtShowWorkout.setText(workouts.get(0).workout_name – Manuel Mato Dec 14 '21 at 01:12

1 Answers1

1

You are printing out the Workout object's default string representation. Assuming you want to print the workout name, you would change

txtShowWorkout.setText(workouts.get(0).toString());

to

txtShowWorkout.setText(workouts.get(0).workout_name);

If you want to print something other than the name, you can either override toString() on the Workout class to print what you want, or make a method like

String workoutTitle(Workout w) {
    // make the string you want to show
    return w.workout_name + " (" + w.focus_area + ")";
}

then use

txtShowWorkout.setText(workoutTitle(workouts.get(0)));
Tyler V
  • 9,694
  • 3
  • 26
  • 52