vendredi 27 novembre 2015

Android: SQLite cursor.getPosition() returns string?

c.getString(c.getPosition()) returns 1 for first element, He for second element, Lithium for third element, and 9.0122 for fourth element.

These are very weird position values to me and I am trying to make sense of this.

Should it be returning the atomicNumber which was set as PRIMARY KEY? I am new to Android, in the documentation it says that cursor.getPosition():

"Returns the current position of the cursor in the row set"

Does this mean it returns the ROWID or the current position in the column?

public void Test(){
    String URL = "content://com.apress.gerber.calculator.PeriodicTable/elements";
    Uri element = Uri.parse(URL);

    Cursor c = getContentResolver().query(element, null, null, null, PeriodicTable.ATOMIC_NUMBER);

    if (c.moveToFirst()) {
        do{
            Toast.makeText(this,
                    c.getString(c.getColumnIndex(PeriodicTable.ATOMIC_NUMBER)) +
                            ", " +  c.getString(c.getColumnIndex( PeriodicTable.SYMBOL)) +
                            ", " + c.getString(c.getColumnIndex( PeriodicTable.NAME)) +
                            ", " + c.getString(c.getColumnIndex(PeriodicTable.MOLAR_MASS)) +
                            ", POSITION: " + c.getString(c.getPosition()),
                    Toast.LENGTH_SHORT).show();
        } while (c.moveToNext());
    }
    else Toast.makeText(this, "Database is empty.", Toast.LENGTH_SHORT).show();

}

Element.xml that is parsed into the SQLite database:

<elements>
    <element>
        <molarMass>1.0079</molarMass>
        <name>Hydrogen</name>
        <symbol>H</symbol>
        <atomicNumber>1</atomicNumber>
    </element>

    <element>
        <molarMass>4.0026</molarMass>
        <name>Helium</name>
        <symbol>He</symbol>
        <atomicNumber>2</atomicNumber>
    </element>

    <element>
        <molarMass>6.941</molarMass>
        <name>Lithium</name>
        <symbol>Li</symbol>
        <atomicNumber>3</atomicNumber>
    </element>

    <element>
        <molarMass>9.0122</molarMass>
        <name>Beryllium</name>
        <symbol>Be</symbol>
        <atomicNumber>4</atomicNumber>
    </element>
</elements>

SQLite table statement:

static final String CREATE_DB_TABLE =
        " CREATE TABLE " + ELEMENTS_TABLE_NAME +
                " ( atomicNumber INTEGER PRIMARY KEY ASC, " +
                " symbol TEXT NOT NULL, " +
                " name TEXT NOT NULL, " +
                " molarMass TEXT NOT NULL);";

Aucun commentaire:

Enregistrer un commentaire