0

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

 // 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 中改变.. 谢谢

4

3 回答 3

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
    }

});
于 2013-11-06T10:50:13.027 回答
0

试试这个代码:

String selecteditem = spinner.getselecteditem().tostring();
textview.settext(Selecteditem);
于 2013-11-06T10:48:31.437 回答
0

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

于 2013-11-06T10:48:42.640 回答