在文本视图中显示微调器的选择

时间:2013-11-06 10:46:00

标签: java android android-sqlite textview android-spinner

我有一个数据库,并且我可以在我的微调器中显示一些值。我如何在textview中显示我选择的内容?

 // 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;

    }

我试过

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

但它始终显示Altro,如果我选择其他不在textview中更改的内容..谢谢

3 个答案:

答案 0 :(得分:2)

在您的活动中使用OnItemSelectedListener():

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
    }

});

答案 1 :(得分:0)

试试这段代码:

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

答案 2 :(得分:0)

Spinner有一个名为setonitemselectedlistener()的默认类,因为我们在该方法中有一个名为onItemSelected()的方法,你可以编写这个设置文本值。