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 = new MyOnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
returnIntent.putExtra("deleteAtIndex",idx);
setResult(RESULT_OK, returnIntent);
finish();
}
};
for (int i =0;i<buttonList.size();i++) {
buttonList.get(i).setText("Remove");
buttonList.get(i).setOnClickListener(onClickListener);
}
How does my implementation of the OCL need to look like ?
Currently I have this:
public class MyOnClickListener implements OnClickListener{
int index;
public MyOnClickListener(int index)
{
this.index = index;
}
@Override
public void onClick(View arg0) {
}
}
However, I am unsure of what I need to do within the constructor of my OCL, aswell as the overriden onClick
function.
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();
}