微调框不显示数据

时间:2018-09-30 12:32:20

标签: android android-spinner

我在程序中使用了一个微调器,它从我的Azure SQL数据库中获取数据,但是当我单击下拉列表时,什么都没有发生。这是我的代码

    try {
        ConnectionHelper conStr = new ConnectionHelper();
        connect = conStr.connectionclass();
        String query = "select * from fabric";
        Statement stmt = connect.createStatement();
        ResultSet rs = stmt.executeQuery(query);
        ArrayList<String> data = new ArrayList<String>();
        while (rs.next()) {
            String id = rs.getString("FABRIC_NAME");
            data.add(id);
        }
        String[] array = data.toArray(new String[0]);
        ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, data);
        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();
            Toast.makeText(Calc_140_plain.this, name, Toast.LENGTH_SHORT)
                    .show();
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

我似乎也找不到LogCat中的错误

1 个答案:

答案 0 :(得分:0)

确保您的数据数组中包含一些元素

while (rs.next()) {
            String id = rs.getString("FABRIC_NAME");
            data.add(id);
        }
    Log.d("data","size:"+data.size());

检查数据后的打印尺寸不为空。

尝试一下:

ArrayAdapter NoCoreAdapter = new ArrayAdapter<String>(context,
                android.R.layout.simple_list_item_1, data);

代替:

ArrayAdapter NoCoreAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, data);