gpt4 book ai didi

android - 自定义微调器适配器上的 setOnItemSelectedListener 不起作用

转载 作者:行者123 更新时间:2023-11-30 02:25:26 36 4
gpt4 key购买 nike

大家好,希望你能帮助我,我有一个自定义的 ArrayAdapter 用于我的微调器,我在 AsyncTask< 中的 onPostExecute() 中设置了 Adapter/。问题是当我调用 setOnItemSelectedListener() 时它无法识别点击事件。我的微调器就像一个对话框,它工作正常,项目列表显示在弹出式微调器中,但项目选择器不起作用。我想这是因为我错过了 setOnItemSelectedListener() 中的 Context 但不确定关于那个。

这是我的代码:

onPostExecute:

protected void onPostExecute(String result){
pDialog.dismiss();

spinAdmin.setAdapter(new MyAdapter(getApplication(), R.layout.opina_spinner_item_saf,safList));

spinAdmin.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parentView,
View view, int position, long id) {

Toast.makeText(getBaseContext(),"MyItem",
Toast.LENGTH_SHORT).show();
}

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

});
}

在同一个 Activity 我的 Adapter 类中:

public class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context ctx, int txtViewResourceId, ArrayList<String> objects) {
super(ctx, txtViewResourceId, objects);
}

@Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
@Override
public View getView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}

public View getCustomView(int position, View cnvtView, ViewGroup prnt) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.opina_spinner_item_saf, prnt, false);

TextView main_text = (TextView) mySpinner .findViewById(R.id.txt_spinner_item_saf);
main_text.setText(safList.get(position));

return mySpinner;

}

}

最佳答案

试试这个:

spinAdmin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}

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

}
});

关于android - 自定义微调器适配器上的 setOnItemSelectedListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27953937/

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