gpt4 book ai didi

android - 将 OnItemSelectedListener 添加到 Spinner

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:18 25 4
gpt4 key购买 nike

我有一个按钮和一个微调器(最初是隐藏的)。当用户按下按钮时,微调器会填充项目并变得可见。现在我想将 OnItemSelectedListener 添加到微调器。我试过很多教程都没有成功。

这是我的 OnCreate 函数

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button load_routes = (Button)findViewById(R.id.load_routes);
Spinner routes = (Spinner)findViewById(R.id.routes_list);

load_routes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_routes(v);
}
});

routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
{
Log.v("routes", "route selected");
}

public void onNothingSelected(AdapterView<?> arg0)
{
Log.v("routes", "nothing selected");
}
});
}

这是我的 load_routes 函数

private void load_routes(View v)
{
Spinner routes = (Spinner)findViewById(R.id.routes_list);
List<String> routes_list = RouteParser.get_routes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
routes.setAdapter(adapter);

TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
list_of_routes_row.setVisibility(View.VISIBLE);
}

此设置不起作用。我让它工作的唯一方法是将我的监听器设置为 routes.setOnItemSelectedListener(this) 然后我实现 OnItemSelectedListener 并包含必要的功能。但是我有多个微调器,需要为不同的微调器创建单独的监听器。任何帮助将不胜感激。谢谢!

最佳答案

    final String[] s2 = getResources().getStringArray(R.array.capteur_size);
final EditText ed = (EditText) findViewById(R.id.editTextCoC);

spinnerCoC = (Spinner) findViewById(R.id.spinnerCoC);

spinnerCoC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {

ed.setText(s2[arg2]);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});

关于android - 将 OnItemSelectedListener 添加到 Spinner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9857127/

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