Reputation: 23381
I have 4 buttons on a layout, and for each of the buttons, they have the attribute:
android:onClick="numberChosen"
now, what I had planned on doing was that in my activity (which uses a layout that has the 4 buttons), I have something like:
public class Blah extends Activity{
String fileName;
Button one;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_number_players_selection_screen);
one = (Button) findViewById(R.id.number_players_one);
}
public void numberChosen(View v){
if(v == one){ // <-------- first way
System.out.println("first way");
}
if(v.getId()==R.id.number_players_one){ // <-------- second way
System.out.println("second way");
}
}
}
if you take note of what happens in the method numberChosen
, between these two ways, which one is better? or are they both exactly doing the same thing?
on a side note, is doing android:onClick="numberChosen"
any better or worse than just setting the View.onClickListener
instead?
Upvotes: 10
Views: 11788
Reputation: 122
I am using min SDK 14 and this solution is not working in some cases (work only for first row of my list adapter)
findViewById(R.id.item_icon_favourite) == view
solved by comparing with IDs
R.id.item_icon_favourite == view.getId()
Upvotes: 5
Reputation: 4383
From my point of view, both ways are correct, they get the job done. From performance point view, the second way might be better.
In addition, like some suggested, using the switch case might not be possible if you are using ADT 14 or higher, in a library project, because the IDs are not final (constants), so you have to use if statements in that case only.
Upvotes: 5
Reputation: 8546
public class Blah extends Activity implements OnClickListener{
String fileName;
Button one;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_number_players_selection_screen);
one = (Button) findViewById(R.id.number_players_one);
one..setOnClickListener(this); // way 1 to do that
// more better way would be customized for whole app
**one..setOnClickListener(new MyClickListener());**
}
/** The click event observer pattern for the different items on the view
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.number_players_one:
// best way 1
break;
default :
break;
}
}
}
A more custom implementation you can have a customized listener for different sections in the app as :
public class MyClickListener implements OnClickListener {
private Context mContext;
@Override
public void onClick(View v) {
mContext = v.getContext();
switch (v.getId()) {
case R.id.number_players_one:
// best way 2 and more better among all
break;
default:
break;
}
}
}
Upvotes: 1
Reputation: 3234
I suggest you to use switch
case its more readable.Its does n't matter weather your are using android:onClick="numberChosen"
or View.onClickListener()
Upvotes: 0