从微调器中填充编辑文本 [英] Populate Edit Text from spinner

查看:51
本文介绍了从微调器中填充编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理这样的代码,例如,用户选择了一个名为FabricA的结构,当他选择它时,它需要使用FabricA价格填充一个编辑文本.我已经尝试实现它,但是我无法获得要在编辑文本中显示的价格.我将名称和价格存储在两个不同的数组中.我应该将其存储在同一阵列中吗?

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?

这是我的代码:

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

推荐答案

在活动类中声明HashMap实例成员

Declare HashMap instance member in your activity class

private HashMap<String,String> hashmap;

然后像这样更改代码:

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            });

这篇关于从微调器中填充编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆