从微调器填充编辑文本

时间:2018-09-30 13:15:25

标签: android android-spinner

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

这是我的代码:

def Graph(placeholder):
    # I want to store the first value of placeholder in "u"
    u = tf.get_variable('x', dtype=tf.float32, trainable=False)


    # Some calculation including 'u'
    # For example, y = 0.7 * placeholder - u

    return y

该代码不会填充我的编辑文本

2 个答案:

答案 0 :(得分:0)

以下是有关如何设置自定义适配器的示例

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

将根据覆盖的toString()

填充值

您可以这样读取值:

(spinner_title.selectedItem as TitleBean).id

以上示例中使用的示例TitleBean

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

注意:该示例在kotlin lang中。但是,想法是一样的。

答案 1 :(得分:0)

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

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