I have an ArrayList of Buttons where my OCL needs to know which index I has been pressed. The plan is something like this:
MyOnClickListener onClickListener
change your for loop and create new Object every time and pass the index in constructor.
for (int i =0;i<buttonList.size();i++) {
buttonList.get(i).setText("Remove");
buttonList.get(i).setOnClickListener(new MyOnClickListener(i));
}
You can get index in your onClick Method.
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Button",v.getText().tostring);
}
});
you will get your button value in view so, you will find it which index is.
set setOnClickListener
to Button as :
buttonList.get(i).setOnClickListener(new MyOnClickListener(i));
EDIT :
I need to finish an activity in myOCL, how would I do that?
for finishing Activity on Button Click from non Activity class you will need to pass Activity Context to your custom OnClickListener as :
buttonList.get(i).setOnClickListener(new MyOnClickListener(i, Your_Current_Activity.this));
and change the Constructor of your custom OnClickListener class to :
int index;
Context context;
public MyOnClickListener(int index,Context context)
{
this.index = index;
this.context=context;
}
@Override
public void onClick(View arg0) {
// now finish Activity as\
context.finish();
// OR
// ((Activity)context).finish();
}
I'm not sure this applies specifically to your case, but I had the desire to create a custom listener for clickable elements to prevent them being clicked twice (if the user taps quickly).
My solution was to create a listener class that I pass a custom Runnable to and then handle that Runnable if the button is being clicked the first time, but obviously you can pass any custom behavior in, this is just one very simple illustration of how to use it...
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;
public class MyCustomOnClickListener implements View.OnClickListener {
public static final String TAG = MyCustomOnClickListener.class.getSimpleName();
private Runnable doOnClick;
private Context mContext;
private int isClicked = 0;
public MyCustomOnClickListener(Context c, Runnable doOnClick) throws Exception{
if(!(c instanceof Context) || !(doOnClick instanceof Runnable))
throw new Exception("MyCustomOnClickListener needs to be invoked with Context and Runnable params");
this.doOnClick = doOnClick;
this.mContext = c;
}
@Override
public void onClick(View view) {
Log.v(TAG,"onClick() - detected");
if(isClicked++ > 0)return; //this prevents multiple clicks
Log.d(TAG, String.format("onClick() - being processed. View.Tag = \"%s\"", view.getTag().toString()));
new Handler(mContext.getMainLooper()).post(doOnClick);
}
}
and then to use it (assuming you're in an Activity for the this
context)...
try{
Button aButton = findViewById(R.id.someButton);
aButton.setOnClickListener(new MyCustomOnClickListener(/*context*/this, new Runnable(){
public void run(){
//here you would put whatever you would have otherwise put in the onClick handler. If you need the View that's being clicked you can replace the Runnable with a custom Runnable that passes the view along
}
}));
}catch(...){
}