我试图在我的listview的基础适配器中设置click侦听器 这是来自服务器,我使用以下代码:
我在基础适配器中的getview:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder = new Holder();
View view;
view = inflater.inflate(R.layout.list_item_curr, null);
holder.tv_title = (TextView) view.findViewById(R.id.tv_title);
holder.tv_id = (TextView) view.findViewById(R.id.tv_id);
holder.relative = (RelativeLayout) view.findViewById(R.id.relative);
Nav_List current=data.get(position);
holder.tv_title.setTypeface(customfont);
holder.tv_title.setText(current.getTitle());
holder.tv_id.setText(current.getDes());
holder.relative.setOnClickListener(this);
holder.relative.setTag(position);
return view;
}
}
我的点击:
@Override
public void onClick(View view) {
int pos = (Integer) view.getTag();
Object object = getItem(pos);
Nav_List beanclass = (Nav_List) object;
switch (view.getId()){
case R.id.relative:
// Toast.makeText(context, "Clicked :" + beanclass.getSku(), Toast.LENGTH_SHORT).show();
Intent in = new Intent(context,Baby_Cat.class);
in.putExtra("Id",beanclass.getDes());
context.startActivity(in);
break;
}
}
但onclick()中存在抛出错误。错误在
之下10-07 11:47:26.245 15371-15371/com.kamalaminfo.asm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kamalaminfo.asm, PID: 15371
java.lang.ClassCastException: java.lang.Integer cannot be cast to com.kamalaminfo.asm.Getter_Setter.Nav_List
at com.kamalaminfo.asm.Adapter.DrawerListAdapter.onClick(DrawerListAdapter.java:80)
at android.view.View.performClick(View.java:5233)
at android.view.View$PerformClick.run(View.java:21211)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
请帮助我找到问题
注意:这个相同的编码在另一个列表视图中正常工作,我很困惑
答案 0 :(得分:0)
它不喜欢整数的转换。 您的对象实际上可能不是您投射到的类。我认为它可能是它在错误中所说的整数。也许你弄错了项目?
尝试在对象上使用getClass并打印它是什么。然后你会看到它是哪个类。
我个人会在onClickListener中使用,而不是“this”,我会编写新的onClickListener()并在点击后实现所需的方法和程序。如果可以的话。喜欢
holder.relative.setOnClickListener(new onClickListener (){
//methods onClick to be implemented
}