mardi 1 mars 2016

Depends spinner getting data from sqlite (Android)

Hi I saw several questions here but I didn't find one that really could help me to solve this issue... I have a SQLITE database already implemented in an Android application, the spinner is already getting the Data from SQLITE, but I have an issue... I need the second spinner to be active just when the user choose his first option in the first spinner, so the second spinner will load the content based on the first selection... For Example I have the column called "Modelos" and have 2 options "Modelo1" and "Modelo2", when the user choose "Modelo1" will show X info in the second spinner, when the user choose "Modelo2" it will show y info on the second spinner... here is my code:

DBackend--

//Make the selection of the first spinner
public String[] getAllSpinnerContent(){

    String query = "Select Modelo from Modelos";
    Cursor cursor = this.getDbConnection().rawQuery(query, null);
    ArrayList<String> spinnerContent = new ArrayList<String>();
    if(cursor.moveToFirst()){
        do{
            String word = cursor.getString(cursor.getColumnIndexOrThrow("Modelo"));
            spinnerContent.add(word);
        }while(cursor.moveToNext());
    }
    cursor.close();

    String[] allSpinner = new String[spinnerContent.size()];
    allSpinner = spinnerContent.toArray(allSpinner);

    return allSpinner;
}


//Make the selection of second spinner

public String[] getAllApelidos () {

    String query = "Select Apelido from Modelos";
    Cursor cursor = this.getDbConnection().rawQuery(query, null);
    ArrayList<String> spinnerApelidos = new ArrayList<String>();

    if(cursor.moveToFirst()){
        do{
            String word = cursor.getString(cursor.getColumnIndexOrThrow("Apelido"));
            spinnerApelidos.add(word);
        } while (cursor.moveToNext());
    }

    cursor.close();

    String[] allApelidos = new String[spinnerApelidos.size()];
    allApelidos = spinnerApelidos.toArray(allApelidos);

    return allApelidos;
}

}

And here is my Activity that show the 2 spinners

package com.rafa.rafa_v2;

import android.content.Context;
import android.database.Cursor;

import java.util.ArrayList;

/**
 * Created by rafaelabueno on 01/03/16.
 */
public class DbBackend extends DbObject{

    public DbBackend(Context context) {
        super(context);
    }

    //Pega o Modelo
    public String[] getAllSpinnerContent(){

        String query = "Select Modelo from Modelos";
        Cursor cursor = this.getDbConnection().rawQuery(query, null);
        ArrayList<String> spinnerContent = new ArrayList<String>();
        if(cursor.moveToFirst()){
            do{
                String word = cursor.getString(cursor.getColumnIndexOrThrow("Modelo"));
                spinnerContent.add(word);
            }while(cursor.moveToNext());
        }
        cursor.close();

        String[] allSpinner = new String[spinnerContent.size()];
        allSpinner = spinnerContent.toArray(allSpinner);

        return allSpinner;
    }

    //Pega o Apelido
    public String[] getAllApelidos () {

        String query = "Select Apelido from Modelos";
        Cursor cursor = this.getDbConnection().rawQuery(query, null);
        ArrayList<String> spinnerApelidos = new ArrayList<String>();

        if(cursor.moveToFirst()){
            do{
                String word = cursor.getString(cursor.getColumnIndexOrThrow("Apelido"));
                spinnerApelidos.add(word);
            } while (cursor.moveToNext());
        }

        cursor.close();

        String[] allApelidos = new String[spinnerApelidos.size()];
        allApelidos = spinnerApelidos.toArray(allApelidos);

        return allApelidos;
    }

 }

If someone could help me, Ill be really glad!

Aucun commentaire:

Enregistrer un commentaire