Mohammed Ismail
Mohammed Ismail

Reputation: 147

Populating a spinner

I am busy with an android application where i get data from my database that a user can choose in the spinner. Here is the code for that:

    spinner = (Spinner) findViewById(R.id.spinnerfabric);
    connect = CONN(un, passwords, db, ip);

    String query = "select * from fabric";
    try {
        connect = CONN(un, passwords, db, ip);
        stmt = connect.prepareStatement(query);
        rs = stmt.executeQuery();
        ArrayList<String> data = new ArrayList<String>();
        while (rs.next()) {
            String id = rs.getString("FabricName");
            data.add(id);
        }
        String[] array = data.toArray(new String[0]);
        ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, data);
        spinnerfabric.setAdapter(NoCoreAdapter);
    } catch (SQLException e) {
        e.printStackTrace();
    }
    spinnerfabric.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            String name = spinnerfabric.getSelectedItem().toString();
            Toast.makeText(ForSpinner.this, name, Toast.LENGTH_SHORT)
                    .show();
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

Now i got this working where i show the fabric names but what i want to do is when the user clicks on fabric i want to display the fabric price on the toast. for example when the user clicks on the spinner for fabricA the toast must show the price of FabricA which is also in my database

Upvotes: 2

Views: 55

Answers (1)

Kinjal Rathod
Kinjal Rathod

Reputation: 549

You can get name by from the list of data using this line :

String name = data.get(position);
Toast.makeText(context,name,Toast.LENGTH_SHORT).show();

Upvotes: 5

Related Questions