Onclicklistener for a button in a list

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?

+5
source share
3 answers

Try to change

 public View getView(int position, View convertView, ViewGroup parent) { ... @Override public void onClick(View v) { View parentRow = (View) v.getParent(); ListView listView = (ListView) parentRow.getParent(); ... } 

to

 public View getView(int position, View convertView, final ViewGroup parent) { ... @Override public void onClick(View v) { ListView listView = (ListView) parent; ... } 
+3
source

The easiest way is to use a tag.
You can use the following code.

 imgview.setTag(new Integer(position); imgview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final int position = (Integer)v.getTag(); Toast.makeText(context_,position+"",Toast.LENGTH_LONG).show(); } }); 

If you want to save another object in the tag, you can use setTag(int key, Object object) to save the position.

To get a Listview, you can use the following code.

 ListView listview = (ListView) v.getParent() //item view .getParent() //Linear Layout in listview .getParent();//ListView 
+1
source

View parentRow = (View) v.getParent (); ListView listView = (ListView) parentRow.getParent (); You write a listener to view the images, and getting the parent from the image returns a list string. Here parentRow.getParent () is the line in listView, which is the layout, and you send it to listview.

0
source

Source: https://habr.com/ru/post/1239305/


All Articles