Populate Edit Text from spinner

2019-07-30 11:04发布

问题:

I am working on code where for example the user selects a fabric called FabricA and when he selects it, it needs to populate an edit Text with FabricA price. i have tried to implement it but i cant get the price to show in the edit text. I stored the name and price in two different arrays. Should i store it in the same array?

Here is my code:

try {
    ConnectionHelper conStr = new ConnectionHelper();
    connect = conStr.connectionclass();

    if (connect == null) {
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
    } else {
        String query = "select * from cc_fabric";
        stmt = connect.prepareStatement(query);
        rs = stmt.executeQuery();
        ArrayList<String> dataF = new ArrayList<>();
        ArrayList<String> dataFP = new ArrayList<>();
        while (rs.next()) {
            String id = rs.getString("FABRIC_NAME");
            String price = rs.getString("FABRIC_UNIT_PRICE");// value of database
            dataF.add(id);
            dataFP.add(price);
        }
        ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataF);
        Fabric.setAdapter(NoCoreAdapter);
    }
} catch (SQLException e) {
    e.printStackTrace();
}

Fabric.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view,
                                int position, long id) {
        String name = Fabric.getSelectedItem().toString();
        String price = Fabric.getSelectedItem().toString();
        fabricPrice.setText(price);
        Toast.makeText(Calc_140_plain.this, name, Toast.LENGTH_SHORT)
            .show();
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

The code doesnt populate my edit text

回答1:

Declare HashMap instance member in your activity class

private HashMap<String,String> hashmap;

Then change your code like :

try {
            ConnectionHelper conStr = new ConnectionHelper();
            connect = conStr.connectionclass();

            if (connect == null) {
                Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
            } else {
                String query = "select * from cc_fabric";
                stmt = connect.prepareStatement(query);
                rs = stmt.executeQuery();
                ArrayList<String> dataF = new ArrayList<>();
                hashMap = new HashMap<>();
                while (rs.next()) {
                    String id = rs.getString("FABRIC_NAME");
                    String price = rs.getString("FABRIC_UNIT_PRICE");// value of database
                    hashMap.put(id,price);
                    dataF.add(id);
                }
                ArrayAdapter NoCoreAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, dataF);
                Fabric.setAdapter(NoCoreAdapter);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }



        Fabric.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                String name = Fabric.getSelectedItem().toString();
               String price = hashMap.get(name);
               fabricPrice.setText(price);
                Toast.makeText(Calc_140_plain.this, name, Toast.LENGTH_SHORT)
                        .show();
            }
            public void onNothingSelected(AdapterView<?> parent) {
            }sss            });


回答2:

Here is an example on how to set a custom adapter

val titles = mutableListOf<TitleBean>()
titles.addAll(allTitleTypes)
spinner_title.adapter = ArrayAdapter(this, R.layout.item_spinner, titles)

Values will be populated on the basis of the overridden toString()

You can read the value like this:

(spinner_title.selectedItem as TitleBean).id

Sample TitleBean used in above example

data class TitleBean(
        var id: String = "",
        var titleName: String = ""
) {
    @Ignore
    override fun toString(): String {
        return titleName
    }
}

Note: The example is in kotlin lang. However, the idea is same.