I think the question says it all: I want to create a custom OnItemClickListener for a ListView. I want to add and change the parameters for a OnItemClickListener but how can I create an own so it is called if I click on an Item in the ListView?
You create an interface first, then implement a method, where you set the listener in your custom class and already then make a main class to listen for your custom event
Interface:
public interface OnCustomEventListener{
public void onEvent(); //can have parameters
}
method in your e.g. adapter:
private OnCustomEventListener mListener; //field
//setter method
public void setCustomEventListener(OnCustomEventListener eventListener) {
this.mListener=eventListener;
}
listener:
someObjectYouWantToHaveYourCustomListenerToBeAssignedFor.setCustomEventListener(new OnCustomEventListener(){
public void onEvent(){
//do whatever you want to do when the event is performed.
}
});
how to call e.g. from your adapter:
if(this.mListener!=null){
this.mListener.onEvent();
}
P.S. Your custom listener may have as many parameters as you want
Source
Related
I'm trying to make an on-click method for recycle List items.
I have found that you can make it inside the Adapter you have made for the list inside the OnBindViewHolder, and it works, however, I do not have access to the EditTexts that are in the Activity where the Adapter is used , I was wondering if there is a way to import them to be used in the Adapter ? Thank you in advance.
You can do it, You have to use Interface for this purpose.
Interface will pass your data from Adapter to your Activity having that EditText.
Then you update your EditText accordingly.
Create an Interface
public interface DataTransferInterface {
public void onItemClicked(String data);
}
Create Object of Interface in Adapter
DataTransferInterface dtInterface;
Initialize it in Constructor of your adapter
public MyAdapter(DataTransferInterface dtInterface) {
this.dtInterface = dtInterface;
}
When Any item is Clicked in Adapter pass data to Activity
item.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
dtInterface.onItemClicked("Data to be passed here");
}
});
Now in your Activity
implement that interface like:
public class MainActivity extends Activity implements DataTransferInterface {
When you implement Interface in Activity, It will show error to implement its required methods. SO,
#Override
public void onItemClicked(String Data) {
// TODO Update your EditText Here
}
Initialize your adapter like
myAdapter = new MyAdapter(this);
If you find any difficulties, You can read more here
In my activity I have one recyclerview and each item view contains buttons. I want to be able to change some UI elements and other things such as an Array of custom objects for the adapter itself in my activity from the recyclerview adapter. Until now I declared the needed views as static but I found out that it's a terrible practice.
Example: I have the following recyclerview that represents a cart filled with a custom viewholder, from an Array of custom "cart_product" objects. (One of this custom oject's proprieties is "quantity" - represented by the spinner). I want to be able to change the object's "quantity" property by changing the spinner's value from the adapter... How could this be done? And when all the products are removed from cart (by swiping & detected from adapter) I want to display a textvie
ScreenShot
You can use callbacks:
In adapter create an interface:
public interface EventHandler {
void handle(int position) // if u need know position. If no, just create method without params
}
Create an private instance of interface in adapter:
public class YourAdapter extends RecyclerView.Adapter<YourHolder> {
private EventHanlder handler;
}
Implement EventHanlder in activity:
public class Mainacitivity extends Activity implements YourAdapter.EventHandler {
//.....
#Override
void handle (int position) {
// TODO do whatever u want
}
}
Add EventHandler to constructor parameters:
public YourAdapter (List<YourObject> data, EventHandler handler) {
//....
this.handler = handler;
}
When you need to change UI call
handler.hanlde(position);
And, finally pass this when initializing adapter
adapter = new YourAdapter (data, this)
If u need something else (not position), just change signature of handle() method
How can I make onclick listener of recyclerview button in main activity and perform click from main activity.
create a interface in your adapter class and implement it in your activity class.
public interface handleClick
{
public void onFirstButtonClick();
public void onSecondButtonClick();
public void onThirdButtonClick();
}
and when creating your adapter in your activity class pass a additional parameter YourAdapter(yourparamters,this)
and now in your adapter
when you intialize your values from construcor assign this to your inteface
Adapter Class
Youradapter(yourParamters,handleClick)
{
this.handleClick=handleClick;
}
now in your onBindViewHolder create onClickListener for your buttons and on
onClick call interface methods like this
button.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
handleClick.onFirstButtonClick();
}
});)
so the method onFirstButtonClick which you will have to implement in your activity class will be called.
You have to do following thing
Firstly create an interface for listening on click event from adapter to activity.
Second implement that interface in your activity.
Third create the instance of your interface in your activity and pass it to adapter.
Four in adapter call your method of interface using instance received at adapter and do your work in your activity.If you will upload code then i will correct.
in my application i'm create simple interface to detect after click on adapter items into parent activity.after create interface and method into adapter i can not how to use this interface to call listener, for example:
Interface Class:
public interface IOnClickListListener {
public void onClick(boolean click);
}
summurized Adapter:
private static class ViewHolder {
public ViewHolder(View view) {
}
public void fill(final ArrayAdapter<SubjectListStructure> adapter, final SubjectListStructure item, final int position) {
root.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* call interface to listen that */
}
});
}
}
public void setOnClickListener(IOnClickOnNiyazmandihaListListener l){
iOnClickOnNiyazmandihaListListener = l;
}
in this adapter after click on item, my interface must be call. now i want to listen that into activity by this code:
subjects_adapter.setOnClickListener(new IOnClickOnNiyazmandihaListListener() {
#Override
public void onClick(boolean click) {
Toast.makeText(G.currentActivity,"ddddd", Toast.LENGTH_SHORT).show();
}
});
now how to call listener into adapter ?
if you want to trigger/invoke the callback, simply call
boolean click = true
iOnClickOnNiyazmandihaListListener.onLogin(click)
from the adapter class
In my honest opinion, if you intend to register the click event inside an adapter, you must pass the position of item which was clicked, instead of passing a boolean value to check if item was clicked or not. In this way, you will get a number of options to handle the clicked item callback.
Here is a simple implementation from my side according to your use-case.
Your interface class just require a small change as follows:
public interface IOnClickListListener {
public void onClick(int positionOfItem);
}
Inside your adapter class, at the onClick() of your root's setOnClickListener, you must call the interface's method in the following way, considering iOnClickOnNiyazmandihaListListener is a member variable of your adapter class
iOnClickOnNiyazmandihaListListener.onClick(position)
where position is passed as a parameter to setOnClickListener and it is the actual position of item clicked.
In this way, wherever you want this onClick() method to be called, you have to provide implementation of the interface in that class, about what you want to do with the clicked position when onClick() is triggered.
So whenever any list item is clicked, your custom implementation of the onClick() method will be used, and the code which you put inside that implemented method will be called.
Example:
Suppose you implemented this interface in an activity, then it tell you to override the onClick() method of your interface inside that activity, i.e. to provide onClick() implementation
So implementation of onClick() would look similar as follows:
#Override
void onClick(int position){
// do what you want to do with clicked item's position
Toast.makeText(this, "Item at position:" + position + " clicked", Toast.LENGTH_LONG).show();
}
For that, your class must have implements IOnClickOnNiyazmandihaListListener as its starting line.
This is a my use case. I have an activity with a listview and a textview. The textview is the sum of all numbers on the listview, the listview has all the numbers.I have a custom adapter for this case.
On each row of the listview, I have a button. This button will change the number on this row.
what I want to do is this:
when user clicks the button, the value on each row is changed - doable.
the sum on the textview also changes accordingly.
This is a simplified example. In reality, I also have a constraint that I have a data model to represent the data on each role. I am not able to extend the data model to DataSetObserver.
any help?
You should create a listener, which listens for touch on your increment button and inside it call a method of the class extending an interface you created.
To make it easy, in your adapter you will have:
public interface OnIncrementListener{
onNumberIncremented();
}
private OnIncrementListener mListener;
//This is inside getView method of your adapter
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mListener.onNumberIncremented();
}
});
Then your activity
public class MyActivity extends Activity implements OnIncrementListener {
//inside onCreate
myAdapter.setOnIncrementListener(this);
//end onCreate
#Override
public void onNumberIncremented() {
//Change value of your TextView here
}
}