gpt4 book ai didi

android - OnItemSelectedListener 不适用于微调器

转载 作者:行者123 更新时间:2023-11-29 16:03:20 25 4
gpt4 key购买 nike

我有一个使用自定义 ArrayAdapter 设置的微调器:

    private static class CustomAdapter<T> extends ArrayAdapter<String> {
public CustomAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}

public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText("");
return view;
}

它初始化如下(Spinner 微调器;语句在上面作为类变量):

    this.spinner = (Spinner) findViewById(R.id.spinner1);
CustomAdapter<String> adapter = new CustomAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, new String[] {"Set Homepage"});

spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);

我已经实现了 OnItemSelectedListener:

public class MainActivity extends Activity implements OnItemSelectedListener{...}

并具有所需的回调:

    //spinner methods
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
//if (pos == 1){
Toast.makeText(this, "Person wants to change the homepage", Toast.LENGTH_SHORT).show();
//}
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
//Toast.makeText(this, "Person wants to change the homepage", Toast.LENGTH_SHORT).show();

}

微调器的 xml:

            <Spinner
android:id="@+id/spinner1"
android:layout_width="45dp"
android:layout_height="45dp"
android:background="@drawable/ic_menu_moreoverflow_holo_dark" />

问题是,无论何时从微调器中选择一个项目,都不会发生任何事情,即使在我删除了所有条件之后,如您所见。

最佳答案

OnItemSelectedListener not working for spinner

因为您只传递 Adapter 中默认选中的一项。可能您在启动应用程序时收到 toast 消息。

因此添加更多元素来检查 OnItemSelectedListener 行为。

关于android - OnItemSelectedListener 不适用于微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21845424/

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