从SQLite DB中获取数据不返回字符串

时间:2018-05-20 19:39:36

标签: android sqlite

当我想使用游标从SQLite数据库中获取数据时,找到的数据集的数量是正确的,但输出是错误的(或者至少不是我需要它的方式):它看起来像这样:< / p>

  

com.example.plabsprojects.mtgcommanderlifecounter.DB_Memo@eb38ae1

我在数据库中有三个不同的表。有人告诉我要覆盖toString()

像这样的方法:

@Override
public String toString(){
    String output = "ID: " + id_actions + ", Name: " + action_name;
    return output;
    }

问题是,我需要在这个String中调用不同的变量而不知道如何。一般方法改编自教程。但他们没有解释,为什么他们会覆盖toString()

我该如何解决这个问题?

这听起来有点令人困惑......我还在学习:)。

我使用三个类:DB_Memo,DB_DataSource和DB_Helper。代码如下:)

DB_MEMO:

public class DB_Memo {

    //Variables for Table Player
    private long id_player;
    private String player_name;

    //Variables for Table Actions
    private long id_actions;
    private String action_name;
    private int action_points;

    //Variables for Table Tracking
    private long id_tracking;
    private long id_player_tracking;
    private long id_actions_tracking;
    private long match_id;
    private String created_at;

    public DB_Memo(long id_player, String player_name){
        this.id_player = id_player;
        this.player_name = player_name;
    }

    public DB_Memo(long id_actions, String action_name, int action_points){
        this.id_actions = id_actions;
        this.action_name = action_name;
        this.action_points = action_points;
    }

    public DB_Memo(long id_tracking, long id_player_tracking, long id_actions_tracking, long match_id, String created_at){
        this.id_tracking = id_tracking;
        this.id_player_tracking = id_player_tracking;
        this.id_actions_tracking = id_actions_tracking;
        this.match_id = match_id;
        this.created_at = created_at;
    }

    //Setter
    public void setId_player(){
        this.id_player = id_player;
    }
    public void setPlayer_name(){
        this.player_name = player_name;
    }

    public void setId_actions(){
        this.id_actions = id_actions;
    }
    public void setAction_name(){
        this.action_name = action_name;
    }
    public void setAction_points(){
        this.action_points = action_points;
    }

    public void setId_tracking(){
        this.id_tracking = id_tracking;
    }
    public void setId_player_tracking(){
        this.id_player_tracking = id_player_tracking;
    }
    public void setId_actions_tracking(){
        this.id_actions_tracking = id_actions_tracking;
    }
    public void setMatch_id(){
        this.match_id = match_id;
    }
    public void setCreated_at(){
        this.created_at = created_at;
    }

    //Getter
    public long getId_player(){return id_player;}
    public String getPlayer_name(){return player_name;}

    public long getId_actions(){return id_actions;}
    public String getAction_name(){return action_name;}
    public int getAction_points(){return action_points;}

    public long getId_tracking(){return id_tracking;}
    public long getId_player_tracking(){return id_player_tracking;}
    public long getId_actions_tracking(){return id_actions_tracking;}
    public long getMatch_id(){return match_id;}
    public String getCreated_at(){return created_at;}

    //    @Override
    //    public String toString(){
    //        String output = "ID: " + id_actions + ", Name: " + action_name;
    //        return output;
    //        }

}

DB_DataSource:

public class DB_DataSource {

    private static final String LOG_TAG = DB_DataSource.class.getSimpleName();

    private SQLiteDatabase database;
    private DB_Helper db_helper;

    private String[] columns_player = {
            DB_Helper.COLUMN_ID_PLAYER,
            DB_Helper.COLUMN_PLAYER_NAME
    };

    private String[] columns_actions = {
            DB_Helper.COLUMN_ID_ACTIONS,
            DB_Helper.COLUMN_ACTION_NAME,
            DB_Helper.COLUMN_ACTION_POINTS,
    };

    private String[] columns_tracking = {
            DB_Helper.COLUMN_ID_TRACKING,
            DB_Helper.COLUMN_TRACKING_PLAYER_ID,
            DB_Helper.COLUMN_TRACKING_ACTION_ID,
            DB_Helper.COLUMN_TRACKING_MATCH_ID,
            DB_Helper.COLUMN_TIMESTAMP,
    };

    public DB_DataSource(Context context){
        Log.d(LOG_TAG, "DataSource erzeugt jetzt den DB_Helper.");
        db_helper = new DB_Helper(context );
    }

    public void open(){
        Log.d(LOG_TAG, "Eine Referenz auf die Datenbank wird jetzt angefragt.");
        database = db_helper.getWritableDatabase();
        Log.d(LOG_TAG, "Datenbank-Referenz erhalten. Pfad zur Datenbank." + database.getPath());
    }

    public void close(){
        db_helper.close();
        Log.d(LOG_TAG, "Datenbank mithilfe des DBhelpers geschlossen.");
    }

    public DB_Memo createPlayer(String player_name){

        ContentValues values = new ContentValues();
        values.put(DB_Helper.COLUMN_PLAYER_NAME, player_name);

        long insertID = database.insert(DB_Helper.TABLE_PLAYER_NAMES, null, values);

        Cursor cursor = database.query(DB_Helper.TABLE_PLAYER_NAMES, columns_player, DB_Helper.COLUMN_ID_PLAYER + "=" + insertID,
                null, null, null, null);

        cursor.moveToFirst();
        DB_Memo db_memo = cursorToDB_Memo(cursor, 1);
        cursor.close();

        return db_memo;
    }

    public DB_Memo createAction(String action_name, int action_points){

        ContentValues values = new ContentValues();
        values.put(DB_Helper.COLUMN_ACTION_NAME, action_name);
        values.put(DB_Helper.COLUMN_ACTION_POINTS, action_points);

        long insertID = database.insert(DB_Helper.TABLE_ACTIONS, null, values);

        Cursor cursor = database.query(DB_Helper.TABLE_ACTIONS, columns_actions, DB_Helper.COLUMN_ID_ACTIONS + "=" + insertID,
                null, null, null, null);

        cursor.moveToFirst();
        DB_Memo db_memo = cursorToDB_Memo(cursor, 2);
        cursor.close();

        return db_memo;
    }

    private DB_Memo cursorToDB_Memo(Cursor cursor, int table){

        if(table==1) {
            int idIndex = cursor.getColumnIndex(DB_Helper.COLUMN_ID_PLAYER);
            int idPlayer_name = cursor.getColumnIndex(DB_Helper.COLUMN_PLAYER_NAME);

            String player_name = cursor.getString(idPlayer_name);
            long id = cursor.getLong(idIndex);

            DB_Memo db_memo = new DB_Memo(id, player_name);

            return db_memo;
        }
        else if (table == 2){
            int idIndex = cursor.getColumnIndex(DB_Helper.COLUMN_ID_ACTIONS);
            int idaction_name = cursor.getColumnIndex(DB_Helper.COLUMN_ACTION_NAME);
            int idaction_points = cursor.getColumnIndex(DB_Helper.COLUMN_ACTION_POINTS);

            String action_name = cursor.getString(idaction_name);
            long id = cursor.getLong(idIndex);

            DB_Memo db_memo = new DB_Memo(id, action_name, idaction_points);

            return db_memo;

        }

        else {
            int idIndex = cursor.getColumnIndex(DB_Helper.COLUMN_ID_PLAYER);
            int idPlayer_name = cursor.getColumnIndex(DB_Helper.COLUMN_PLAYER_NAME);

            String player_name = cursor.getString(idPlayer_name);
            long id = cursor.getLong(idIndex);

            DB_Memo db_memo = new DB_Memo(id, player_name);

            return db_memo;
        }
    }

    public List<DB_Memo> getAllPlayerNames(){
        List<DB_Memo> db_memoList = new ArrayList<>();

        Cursor cursor = database.query(DB_Helper.TABLE_PLAYER_NAMES, columns_player,
                null, null, null, null, null);

        cursor.moveToFirst();
        DB_Memo db_memo;

        while(!cursor.isAfterLast()){
            db_memo = cursorToDB_Memo(cursor ,1);
            db_memoList.add(db_memo);
            cursor.moveToNext();
        }

        cursor.close();

        return db_memoList;
    }

    public List<DB_Memo> getAllActions(){
        List<DB_Memo> db_memoList = new ArrayList<>();
        Cursor cursor = database.query(DB_Helper.TABLE_ACTIONS, columns_actions,
                null, null, null, null, null);

        cursor.moveToFirst();
        DB_Memo db_memo;

        while(!cursor.isAfterLast()){
            db_memo = cursorToDB_Memo(cursor, 2);
            db_memoList.add(db_memo);
            cursor.moveToNext();
        }

        cursor.close();

        return db_memoList;
    }
}

DB_Helper:

public class DB_Helper extends SQLiteOpenHelper {

    public static final String DB_NAME = "mtg_league.db";
    public static final int DB_VERSION = 1;

    //TABLE PLAYER
    public static final String TABLE_PLAYER_NAMES = "table_player";
    public static final String COLUMN_ID_PLAYER = "_id";
    public static final String COLUMN_PLAYER_NAME = "player_name";

    public static final String SQL_CREATE_PLAYER =
            "CREATE TABLE " + TABLE_PLAYER_NAMES +
                    "(" + COLUMN_ID_PLAYER + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_PLAYER_NAME + " TEXT NOT NULL);";

    //TABLE ACTIONS
    public static final String TABLE_ACTIONS = "table_actions";
    public static final String COLUMN_ID_ACTIONS = "_id";
    public static final String COLUMN_ACTION_NAME = "action_name";
    public static final String COLUMN_ACTION_POINTS = "action_points";

    public static final String SQL_CREATE_ACTIONS =
            "CREATE TABLE " + TABLE_ACTIONS +
                    "(" + COLUMN_ID_ACTIONS + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_ACTION_NAME + " TEXT NOT NULL, " +
                    COLUMN_ACTION_POINTS + " INTEGER NOT NULL);";

    //TABLE TRACKING
    public static final String TABLE_TRACKING = "table_tracking";
    public static final String COLUMN_ID_TRACKING = "_id";
    public static final String COLUMN_TRACKING_PLAYER_ID = "player_id";
    public static final String COLUMN_TRACKING_AGAINST_PLAYER_ID = "against_player_id";
    public static final String COLUMN_TRACKING_ACTION_ID = "action_id";
    public static final String COLUMN_TRACKING_MATCH_ID = "match_id";
    public static final String COLUMN_TIMESTAMP = "timestamp";

    public static final String SQL_CREATE_TRACKING =
            "CREATE TABLE " + TABLE_TRACKING +
                    "(" + COLUMN_ID_TRACKING + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_TRACKING_PLAYER_ID + " TEXT NOT NULL, " +
                    COLUMN_TRACKING_AGAINST_PLAYER_ID + " TEXT, " +
                    COLUMN_TRACKING_ACTION_ID + " TEXT NOT NULL, " +
                    COLUMN_TRACKING_MATCH_ID + " INTEGER NOT NULL, " +
                    COLUMN_TIMESTAMP + " DATETIME DEFAULT CURRENT_TIMESTAMP);";



    public DB_Helper(Context context){
        super(context, DB_NAME, null, DB_VERSION);
        Log.d("Test", "DB Helper hat die Datenbank: " + getDatabaseName() + " erzeugt.");

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try{
            Log.d("Test", "Tabelle wird mit SQL-Befehl " + SQL_CREATE_PLAYER + " angelegt.");
            db.execSQL(SQL_CREATE_PLAYER);
            Log.d("Test", "Tabelle wird mit SQL-Befehl " + SQL_CREATE_ACTIONS + " angelegt.");
            db.execSQL(SQL_CREATE_ACTIONS);
            Log.d("Test", "Tabelle wird mit SQL-Befehl " + SQL_CREATE_TRACKING + " angelegt.");
            db.execSQL(SQL_CREATE_TRACKING);
        }
        catch (Exception ex){
            Log.e("Test", "Fehler beim Anlegen der Tabelle: " + ex.getMessage());
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

1 个答案:

答案 0 :(得分:1)

  

<强> com.example.plabsprojects.mtgcommanderlifecounter.DB_Memo@eb38ae1

对象的(DB_Memo实例)是否为默认 toString 方法。

你应该做的是使用对象的适当的getter方法。例如如果您想 id_actions ,那么您将使用类似

的内容
    String.valueOf(your_memo_instance.getId_actions());

而不是

    your_memo_instance.toString();