gpt4 book ai didi

java - 带有 setOnItemSelectedListener 的微调器不执行方法

转载 作者:行者123 更新时间:2023-11-29 02:28:21 26 4
gpt4 key购买 nike

我创建了一个微调器,它从某个数据库中获取值。微调器还有一个执行方法的 setOnItemSelectedListener。每当我单击微调器上的某个项目时,我都不会执行其中的内容。

    //Getting values from database
myDBJ = new JobDatabaseHelper(this);
ArrayList<String> jobname= myDBJ.getalljobname();

Spinner spinnerJob = (Spinner) findViewById(R.id.spinnerJob);
ArrayAdapter<String> adapterForJob = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, jobname);
adapterForJob.notifyDataSetChanged();
adapterForJob.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerJob.setAdapter(adapterForJob);
spinnerJob.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(InfoActivity.this, "test" + i ,Toast.LENGTH_SHORT).show();
//refreshrate();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});

我什至在其中制作了一条 toast 消息,以便我知道它是否有效。当我单击某个项目时,它不显示 toast 消息。我想补充的另一个细节是,这在一个名为“refreshspinners”的方法中,该方法在我的 onCreate 和 onPostResume 覆盖方法中调用。

最佳答案

你能试试静态字符串列表吗:

ArrayList<String> jobname = (ArrayList<String>) Arrays.asList(new String[] {"Engineer", "Manager", "Business developer"});

Spinner spinnerJob = (Spinner) findViewById(R.id.spinnerJob);
ArrayAdapter<String> adapterForJob = new ArrayAdapter<String>(InfoActivity.this, android.R.layout.simple_spinner_item, jobname);
adapterForJob.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerJob.setAdapter(adapterForJob);

spinnerJob.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(InfoActivity.this, "test" + i ,Toast.LENGTH_SHORT).show();
//refreshrate();
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});

关于java - 带有 setOnItemSelectedListener 的微调器不执行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50988127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com