I have a project in android studio with a custom list. my custom list contains 2textbox and a switch and an image button, but I canβt find out how to set onclicklistener or other listeners for them in the listadapter class
here is my list:
public class listadapter extends ArrayAdapter { Context context_; int resource_; ArrayList<reminders> objects_; boolean bool; DBAdapter db; public listadapter(Context context, int resource, ArrayList<reminders> 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; }
but when I test my project and click on the image, it gives me strength and this error in 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)
What can I do?
source share