Reputation: 113
I am currently using the custom layout for the child in Expandablelist which includes 2 different text views and an imageview. Also it will always have 1 child everytime. I am able to get the clicklistener in the adapter but not able to get this in the activity.
I have also tried to get the click listener event in the onChildClick() method but it was not working.
I am using this adapter in different activities so i want to have the click listener for one of the text view in the activity itself.
Any suggestions please.
Upvotes: 0
Views: 705
Reputation: 113
I am able to get this done guys, i added android:onClick="textClickHandler">
in the layout file and included the listener as below in my activity:
public void textClickHandler(View view){
if(view.getId() == R.id.textMore){
Toast.makeText(mContext, "More Text Clicked.", Toast.LENGTH_LONG).show();
}
}
Upvotes: 2
Reputation: 87064
You can't set the listener on a View
from the Activity
(if I understood what you're trying to do). One way to do it would be to:
boolean
value that you set when you instantiate the adapter(This will tell you if the adapter belongs to an Activity
where the TextView
should have a listener(for true
, for example). If the value is false
then you'll make sure not to set a listener on the TextView
).TextView
(only if the previous value was true
, for other activities you'll simple ignore this part) in the getChildView
method of your custom adapter.onClick
method for that view's OnCLickListener
use the Context
that you pass in the constructor of your custom adapter. Cast it to the desired activity and use it as you want. A more elegant way of doing this would be to let the desired Activity
implement a custom interface(to notify you when the TextView
was clicked). Then in the adapter make a field with the type of that interface that you'll set to point to your Activity
when you instantiate the listener. When the TextView
is clicked call the methods of that field above and notify the Activity
that something happen and it should do some stuff.Upvotes: 1