jeff7 jeff7 - 6 days ago 5
Android Question

retrieve the id of a selected element in a spinner loaded from a sqlite database

The Id that i want to retrieve shall be the same that is in the sqlite database, not the id of the item row of the spinner.

I used the code in the below link and get some exceptions.


load from spinner sqlite with text and value


i think the problem is here:

anId = Integer.valueOf(( (AnSco) spnAnsco.getSelectedItem () ).getIdan());


i tried to do:

anId = ( (AnSco) spnAnsco.getSelectedItem () ).getIdan();


Always the same problem.

To Load Spinner

private void loadSpinnerAnsco() {
List<String> ansco = new ArrayList<String>();
db = new DatabaseHelper(this);
boolean ok = true;
try {
SQLiteDatabase dbs = db.getWritableDatabase();

cursor = dbs.rawQuery(
"Select idan as _id, ansco from TAnSco order by ansco",
null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
ansco.add(cursor.getString(1));
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
dbs.close();

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

// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spnAnsco.setAdapter(dataAdapter);

} catch (Exception ex) {
ok = false;
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage(ex.toString());
b.show();
} finally {
if (ok) {
db.close();
}
}

}


To Get item Selected

public class anscoSpinner implements OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String label = parent.getItemAtPosition(position).toString();
AnSco an=new AnSco(label);
anId = Integer.valueOf(( (AnSco) spnAnsco.getSelectedItem () ).getIdan());
loadSpinnerPeriode(anId);
// Showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + label+ " AnId: "+anId,
Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
// TODO Auto-generated method stub

}


Object Class Ansco

public class AnSco {

int idan;
String ansco;

public AnSco(int idan, String ansco) {
this.idan = idan;
this.ansco = ansco;
}

public AnSco(String ansco) {
this.ansco = ansco;
}

public AnSco() {
// TODO Auto-generated constructor stub
}

public int getIdan() {
return idan;
}
public void setIdan(int idan) {
this.idan = idan;
}

public String getAnsco() {
return ansco;
}
public void setAnsco(String ansco) {
this.ansco = ansco;
}

}


StackTrace :

10-21 13:34:59.319: E/AndroidRuntime(438): FATAL EXCEPTION: main
10-21 13:34:59.319: E/AndroidRuntime(438): java.lang.ClassCastException: java.lang.String
10-21 13:34:59.319: E/AndroidRuntime(438): at com.android.moyenne.activity.MoyenneMain$anscoSpinner.onItemSelected(MoyenneMain.java:306)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.widget.AdapterView.access$200(AdapterView.java:42)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.os.Handler.handleCallback(Handler.java:587)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.os.Handler.dispatchMessage(Handler.java:92)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.os.Looper.loop(Looper.java:123)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-21 13:34:59.319: E/AndroidRuntime(438): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 13:34:59.319: E/AndroidRuntime(438): at java.lang.reflect.Method.invoke(Method.java:521)
10-21 13:34:59.319: E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-21 13:34:59.319: E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-21 13:34:59.319: E/AndroidRuntime(438): at dalvik.system.NativeStart.main(Native Method)
10-21 13:35:02.119: I/Process(438): Sending signal. PID: 438 SIG: 9

Answer

I used this tutorial load from spinner sqlite with text and value and get some exceptions during running.

I used the solution in the link you provided. I suggest the following changes.

In Your DatabaseClass create the following Method

public List < Ansco> getAllLabels(){
    List < Ansco > lansco = new ArrayList < Ansco > ();
    // Select All Query
    String selectQuery = "SELECT  * FROM "  TAnSco;// tAnsco is your table name?

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if ( cursor.moveToFirst () ) {
        do {
            lansco.add ( new Ansco ( cursor.getString(0) , cursor.getString(1) ) );
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning labels
    return lansco;
}

Then in your load spinner method

 List <Ansco> ansco = db.getAllLabels();
 ArrayAdapter<Ansco> dataAdapter = new ArrayAdapter<Ansco>(this,
                android.R.layout.simple_spinner_item, ansco);
 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 spnAnsco.setAdapter(dataAdapter);

Then in your spinner selection

  int databaseId = Integer.parseInt ( ( (SpinnerObject) spin2.getSelectedItem () ).getId () );

And Change your Ansco class

public class AnSco {

    String idan; // change to string
    String ansco;

    public AnSco(String idan, String ansco) {
        this.idan = idan;
        this.ansco = ansco;
    }

    public AnSco(String ansco) {
        this.ansco = ansco;
    }

    public AnSco() {
        // TODO Auto-generated constructor stub
    }

    public String getIdan() {
        return idan;
    }
    public void setIdan(String idan) {
        this.idan = idan;
    }

    public String getAnsco() {
        return ansco;
    }
    public void setAnsco(String ansco) {
        this.ansco = ansco;
    }

}
Comments