android Spinner OnItemSelectedListener监听无效

qin123q 2014-07-24 10:50:26

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.register);
info.WebGet();
nations=info.initData(); //这个有数据,这里没有问题
NationSpinner=(Spinner) findViewById(R.id.NationText);
MyAdapter myAdapter=new MyAdapter(this,loadRes,nations);
NationSpinner.setAdapter(myAdapter);
NationSpinner.setOnItemSelectedListener(myAdapter);
}

上面是Activity的代码,
下面是 适配器的类;


public class MyAdapter extends BaseAdapter implements OnItemSelectedListener {

Context context;
ArrayList<Nation> list=new ArrayList<Nation>();
private Handler handler;
public MyAdapter(Context con,Handler handler,ArrayList<Nation> list){
this.list=list;
context=con;
this.handler=handler;
}
@Override
public int getCount() {
android.util.Log.i("t460470591", "getCount");
return list.size();
}

@Override
public Nation getItem(int position) {
android.util.Log.i("t460470591", "getItem");
return list.get(position);
}

@Override
public long getItemId(int position) {
android.util.Log.i("t460470591", "getItemId");
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
android.util.Log.i("t460470591", "getView "+position);
Nation o=list.get(position);
View view=View.inflate(context, R.layout.item_listview,null);
TextView tView=(TextView) view.findViewById(R.id.nation_id);
TextView tViewD=(TextView) view.findViewById(R.id.nation_text);
tView.setText(o.getNationid()+"");
tViewD.setText(o.getNationtext()+"");
return view;
}

@Override //执行不了这个方法
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
android.util.Log.i("t460470591", "onItemSelected");
TextView iDTextView=(TextView) arg1.findViewById(R.id.nation_id);
TextView TextView=(TextView) arg1.findViewById(R.id.nation_text);
Bundle bundle=new Bundle();
bundle.putString(WebSerState.Operate, WebSerState.GetNation);
bundle.putString(WebSerState.NationID, iDTextView.getText().toString().trim());
bundle.putString(WebSerState.NationText,TextView.getText().toString().trim());
Message msgMessage=new Message();
msgMessage.what=WebSerState.Other;
msgMessage.setData(bundle);
handler.sendMessage(msgMessage);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
android.util.Log.i("t460470591", "onNothingSelected");
}


}

...全文
1009 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
秋千不会坠 2017-03-09
  • 打赏
  • 举报
回复
楼主解决了吗
秋千不会坠 2017-03-09
  • 打赏
  • 举报
回复
我没用线程 自定义了一个适配器 监听器也没响应
qin123q 2014-07-30
  • 打赏
  • 举报
回复
我知道什么原因了,在外部写,内部写都无所谓的 其实,主要是用了线程,当我监听的时候,线程还没有把数据放回来,所以,老是不能成功,只要把数据先读出来,然后在放到adapter里面就可以了
qin123q 2014-07-24
  • 打赏
  • 举报
回复
引用 1 楼 heaimnmn 的回复:
你单独写个OnItemSelectedListener 会不会执行了,,,
单独写貌似可以实现,但是为什么 写在一个类就不行,我别的接口也是这样实现的,别的可以啊
Birds2018 2014-07-24
  • 打赏
  • 举报
回复
在R.layout.item_listview 这个布局文件里面 加入下面的标签试试,可能是没有焦点。 android:descendantFocusability="blocksDescendants" android:focusable="false"
lionfresh 2014-07-24
  • 打赏
  • 举报
回复
OnItemSelectedListener 接口应该在你的Activity中实现吧。
哎,真难 2014-07-24
  • 打赏
  • 举报
回复
你单独写个OnItemSelectedListener 会不会执行了,,,
  • 打赏
  • 举报
回复
你可以在activity类里面写OnItemSelectedListener 。 或者通过适配器的构造方法,传递个Spinner对象过来,在执行OnItemSelectedListener 也是可以的。
灰火 2014-07-24
  • 打赏
  • 举报
回复
这写法好奇怪啊~不能在activity里直接set监听器么?
红袖满楼招 2014-07-24
  • 打赏
  • 举报
回复
要么自定义接口类,要么直接匿名内部类实现,你写到适配器中调用不到的
qin123q 2014-07-24
  • 打赏
  • 举报
回复
我现在发现 写在一个类里面 也不能监听了
qin123q 2014-07-24
  • 打赏
  • 举报
回复
引用 3 楼 birdsaction 的回复:
在R.layout.item_listview 这个布局文件里面 加入下面的标签试试,可能是没有焦点。 android:descendantFocusability="blocksDescendants" android:focusable="false"
家了 不得

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧