Crimeiaman
Crimeiaman

Reputation: 51

Load a row value from a database

-----------------------SOLVED-----------------------

How can I load the values ​​as the row that the users selected? Like this:

Database example

If the user select test (from a Spinner) set EditText to the value, but have more than one value to more than one EditText.

// Labels Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_VALUE = "value";

public List<String> getAllLabels(){
    List<String> labels = new ArrayList<String>();

    String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    return label}


 // MainActivity Spinner
private void loadSpinnerData() {
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());

    List<String> lables = db.getAllLabels();

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                                                                android.R.layout.simple_spinner_item, lables);

    dataAdapter
        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinner.setAdapter(dataAdapter);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
                           long id) {
    String label = parent.getItemAtPosition(position).toString();

    Toast.makeText(parent.getContext(), "You selected: " + label ,
                   Toast.LENGTH_LONG).show();

}

PS: I have the Spinner loaded and database created.

PS²: The user will create the database keys, so I don't know the name of key "name".

Upvotes: 0

Views: 44

Answers (1)

Nigam Patro
Nigam Patro

Reputation: 2770

To get the column keys, you can use this following code.

SQLiteDatabase mDataBase;
mDataBase = getReadableDatabase();
Cursor dbCursor = mDataBase.query(TABLE_NAME, null, null, null, null, null, null);
String[] columnNames = dbCursor.getColumnNames();

Upvotes: 1

Related Questions