我在android studio中有一个带有自定义列表视图的项目.我的自定义列表视图包含2textbox和一个开关和图像按钮,但我无法找到如何在listadapter类中为它们设置onclicklistener或其他监听器
这是我的listadapter:
public class listadapter extends ArrayAdapter { Context context_; int resource_; ArrayListobjects_; boolean bool; DBAdapter db; public listadapter(Context context, int resource, ArrayList objects) { super(context, resource, objects); context_ = context; resource_ = resource; objects_ = objects; } public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context_.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View ViewRow = inflater.inflate(R.layout.reminders_list_layout, parent, false); TextView nametxt, addresstxt; Switch sw; ImageView imgview; nametxt = (TextView) ViewRow.findViewById(R.id.remindername); addresstxt = (TextView) ViewRow.findViewById(R.id.reminderaddress); sw = (Switch) ViewRow.findViewById(R.id.remindersw); imgview = (ImageView) ViewRow.findViewById(R.id.imageView4); nametxt.setText(objects_.get(position).name); addresstxt.setText(objects_.get(position).address); bool = (objects_.get(position).swbool != 0); sw.setChecked(bool); imgview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { View parentRow = (View) v.getParent(); ListView listView = (ListView) parentRow.getParent(); final int position = listView.getPositionForView(parentRow); Toast.makeText(context_,position+"",Toast.LENGTH_LONG).show(); } }); return ViewRow; }
但是当我测试我的项目并点击imageview时,它会给我强制关闭和logcat中的这个错误
12-27 20:58:50.343 5658-5658/com.amir_p.yadambendaz E/AndroidRuntime: FATAL EXCEPTION: main Process: com.amir_p.yadambendaz, PID: 5658 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.ListView at com.amir_p.yadambendaz.listadapter$1.onClick(listadapter.java:49)
我能做什么?