I have a loop where i am created multiple button instances and with each instance they have a TouchListener. However, i am having trouble figuring out how to manage when the different buttons have been touched. What i am trying to accomplish is that each button represents a textview and when i touch the button i want to add that particular textview associated with that instance of the button to an arraylist. However, when the same button is touched again, i want to remove it from the list..and add it back again if it is touched again. Oh yeah addToOrder currently is global and initialized to true.
Thoughts?
EDIT****per my comments below button is now an instance of a subclass of Button that I wrote so that i can easily keep up with each instance of the button. That solved the issue.
here is the code:
button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getActionMasked();
//Log.d("Touched Add To Order Button with id of ", button.getId() + ": " + button.getText().toString());
TextView relativeTitleView;
ViewGroup relativeGroup = (ViewGroup)v.getParent();
relativeTitleView = (TextView) relativeGroup.getChildAt(0);
//Log.d("Add To Order Button Touched", relativeTitleView.getText().toString());
/*if(action ==MotionEvent.ACTION_DOWN){
addToOrder = (addToOrder) ? true : false;
if(addToOrder)
dbAccess.addToOrder(relativeTitleView.getText().toString());
else
dbAccess.removeFromOrder(relativeTitleView.getText().toString());
}
else if(action == MotionEvent.ACTION_UP){
addToOrder = !addToOrder;
}*/
if(action == MotionEvent.ACTION_DOWN){
if(button.getTouchInfo()){
dbAccess.addToOrder(relativeTitleView.getText().toString());
button.setTouchInfo(false);
}
else {
dbAccess.removeFromOrder(relativeTitleView.getText().toString());
button.setTouchInfo(true);
}
}
return false;
}
});
If this is the only way items are added/removed from dbAccess, you can just switch based on whether or not it’s already there.
Edit: Assuming dbAccess has a contains(), can’t tell what type it is here.