David_D
David_D

Reputation: 1402

Display choice from spinner in a textview

I have a db and there there are some values i can display in my spinner.. How can i display in a textview what i've choose?

 // CREATE TABLE IF NOT EXISTS
    public void createTable() {
        try {
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
            mydb.execSQL("CREATE TABLE IF  NOT EXISTS " + TABLE
                    + " (ID INTEGER PRIMARY KEY, NAME TEXT, PLACE TEXT);");
            mydb.close();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Errore durante la creazione della tabella",
                    Toast.LENGTH_LONG);
        }
    }

    // THIS FUNCTION INSERTS DATA TO THE DATABASE
    public void insertIntoTable() {
        try {
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Cibo e Bevande','cibo')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Trasporti','trasporti')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Svago','svago')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Acquisti vari','acquisti')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Sanità','sanità')");
            mydb.execSQL("INSERT INTO " + TABLE
                    + "(NAME, PLACE) VALUES('Altro','altro')");
            mydb.close();


        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),
                    "Error in inserting into table", Toast.LENGTH_LONG);
        }
    }

    // THIS FUNCTION SHOWS DATA FROM THE DATABASE
    public ArrayList<String> getTableValues() {

        ArrayList<String> my_array = new ArrayList<String>();
        try {
            mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
            Cursor allrows = mydb.rawQuery("SELECT * FROM " + TABLE, null);
            System.out.println("COUNT : " + allrows.getCount());

            if (allrows.moveToFirst()) {
                do {

                    String ID = allrows.getString(0);
                    String NAME = allrows.getString(1);
                    String PLACE = allrows.getString(2);
                    my_array.add(NAME);

                    TextView categorietext = (TextView) findViewById(R.id.sceltelabel);
                    categorietext.setText(NAME); // doesn't work. It display always "Altro"

                } while (allrows.moveToNext());
            }
            allrows.close();
            mydb.close();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Errore.",
                    Toast.LENGTH_LONG);
        }
        return my_array;

    }

I tryied with

TextView categorietext = (TextView) findViewById(R.id.sceltelabel);
                        categorietext.setText(NAME);

But it display always Altro and if i choose something else not change in textview.. Thanks

Upvotes: 0

Views: 1196

Answers (3)

VINIL SATRASALA
VINIL SATRASALA

Reputation: 634

Spinner have a default classes called setonitemselectedlistener(), in that we have a method named onItemSelected() in that method you can write this setting text value.

Upvotes: 0

Pankaj Arora
Pankaj Arora

Reputation: 10274

Try this code:

String selecteditem = spinner.getselecteditem().tostring();
textview.settext(Selecteditem);

Upvotes: 0

Rethinavel
Rethinavel

Reputation: 3952

Use OnItemSelectedListener() in your Activity:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        // your code here

   String selecteditem = spinner.getselecteditem().tostring();
   textview.settext(Selecteditem);


    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
    }

});

Upvotes: 2

Related Questions