0

我有一个活动,它从用户那里获取银行名称和帐号。银行名称和相应的帐号存储在数据库中。现在在另一个活动中有两个微调器,其中一个获取银行名称并用它们填充自己,另一个获取与该银行对应的帐号。我成功填充了微调器。我遇到的唯一问题是,第一个微调器中的条目(包含银行名称)不可点击,因此第二个微调器也变得无用。我无法理解为可能是什么原因。这是微调器的代码:

   // in onCreate() function
   // for the spinner holding bank name
    Cursor cursor = myDatabase.getData();
    cursor.moveToFirst();
    myAdapter = new SimpleCursorAdapter(AddTransaction.this,
                R.layout.spinnerlayout, cursor,
                new String[] { DatabaseClass.KEY_BANK_NAME_ID },
                new int[] { R.id.bankName },
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    mySpinner.setAdapter(myAdapter);

    get_datafrom_spinner = cursor.getString(cursor
                .getColumnIndex(DatabaseClass.KEY_BANK_NAME_ID));
        // Toast.makeText(AddTransaction.this, get_datafrom_spinner,
        // Toast.LENGTH_LONG).show();

        // mySpinner.setOnItemSelectedListener(this);

    myDatabase.close();



@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
        long arg3) {
    // TODO Auto-generated method stub
    try {
        myDatabase.open();

        int pos = position;
        Cursor cursor = (Cursor) myAdapter.getItem(pos);
        bank_name = cursor.getString(cursor // name of the bank
                                            // selected
                .getColumnIndex(DatabaseClass.KEY_BANK_NAME_ID));
        myAdapter = new SimpleCursorAdapter(AddTransaction.this,
                R.layout.spinnerlayout, cursor,
                new String[] { DatabaseClass.KEY_BANK_NAME_ID, },
                new int[] { R.id.bankName },
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        mySpinner.setAdapter(myAdapter);

        // Toast.makeText(AddTransaction.this, s,
        // Toast.LENGTH_SHORT).show();

        // returns the account numbers corresponding to this bank name
        Cursor c = myDatabase.getAccountData(bank_name);
        c.moveToFirst();

        myAdapter_for_account = new SimpleCursorAdapter(
                AddTransaction.this, R.layout.account_number_spinner, c,
                new String[] { DatabaseClass.KEY_ACCOUNT_NUMBER_ID },
                new int[] { R.id.accNum },
                SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        mySpinner_for_account.setAdapter(myAdapter_for_account);

        // position of the the clicked in the spinner for account
        int position_account = mySpinner_for_account
                .getSelectedItemPosition();

        Cursor cursor2 = (Cursor) myAdapter_for_account
                .getItem(position_account);

        // holds the account number for the spinner item selected
        account_number_selected = cursor2.getString(cursor2
                .getColumnIndex(DatabaseClass.KEY_ACCOUNT_NUMBER_ID));

        // Toast.makeText(AddTransaction.this, account_number_selected,
        // Toast.LENGTH_LONG).show();
        Cursor balance = myDatabase
                .getAmountfor_Account(account_number_selected);
        balance.moveToFirst();

        /*
         * c = myDatabase.getAmountfor_Account(account_for_spiiner_account);
         * c.moveToFirst();
         */

        // get the balance as a string

        balance_in_account = balance.getString(balance // <--
                .getColumnIndex(DatabaseClass.KEY_CURRENT_BALANCE_ID));

        // Toast.makeText(AddTransaction.this, balance_in_account,
        // Toast.LENGTH_SHORT).show();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    myDatabase.close();

}
4

2 回答 2

0
Spinner s = (Spinner) findViewById(R.id.spinner_id);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView adapter, View v, int i, long lng) {
//do something here
}

@Override
public void onNothingSelected(AdapterView arg0) {
//do something else
}
});
于 2012-10-12T11:04:11.410 回答
0

终于让它工作了,问题是我在同一个 onItemSelected() 函数中声明了两个微调器,每个微调器都应该有自己的 onItemSelected 函数才能工作。

于 2012-10-17T13:28:49.807 回答