Generic-user-small kenneth rooks 2 posts

while going through the example for the sudoku i found it amazingly helpful till i reached the section on implementing an about box. in the beginning you pretty much explained every new element in the code and its usage, then when you came to this section you jumped right into intents and themes with out clear explanation of the new elements in the code. it kind of threw me off. i don’t know if any body has addressed this or not but i thank you in advance for this wonderfully helpful book.

 
Burnette_ed_small Ed Burnette 31 posts

Thanks, after the last beta I did rewrite parts of the sudoku example but I’m not sure if I addressed your concern. Can you point out some particular sentences that were unclear?

 
Generic-user-small kenneth rooks 2 posts

sorry for the wait. when i had the original problem i was reading at 3 in the morning, but the very next day i went through and reread it carefully and everything became clear. sorry for the confusion

 
Burnette_ed_small Ed Burnette 31 posts

No problem.

 
Generic-user-small Chuck Glassmire 1 post

Im actually having this problem now. I just bought the PDF today. Phenomenal job thus far, ive understood everything fine until right about now. Its like ive been smacked with more code then explanation.

Here are my questions:

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

What exactly is going on here and why do i need to do this?

This is where my realy problems begin,

View continueButton = this.findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = this.findViewById(R.id.new_button);
newButton.setOnClickListener(this);
etc etc

i really dont follow. i understand @+id added new id, but what exactly is the code doing? what is onclicklistener? whats (this)?

public class Sudoku extends Activity implements OnClickListener {
// ...
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
// More buttons go here (if any) ...
}
}

and i really just dont have much of a clue here either. im extremely lost when it comes to this section of code. Ive been following fine until now. Thank you for any help.

 
Burnette_ed_small Ed Burnette 31 posts

Well, the imports are important so you’ll get the right classes. Classes that have common names like View and Button are used in many places by different libraries (for example Swing, SWT, and Android). Originally I left out the imports to save space but readers had trouble with their IDE making bad guesses and picking the wrong classes.

In Java a “listener” is a piece of code that gets called when something happens. Other languages might call this a “callback” or an “event handler”. So the line:

newButton.setOnClickListener(this); 

says that when the “newButton” button is clicked, you want the system to call some method on the “this” object so that you can handle that click (to start a new game). “this” is an instance of the class you’re defining, in this case it’s the Sudoku class.

If you look at the documentation for setOnClickListener() it says the first parameter has a type of “OnClickListener”. In other words the first parameter is any object that implements the OnClickListener contract. OnClickListener is a Java interface. If you know Ruby it’s a little like a mixin. The line:

public class Sudoku implements OnClickListener 

is a guarantee to the compiler that you’re going to define all the methods required by OnClickListener. If you look at the source to OnClickListener you’ll see something like this:

interface OnClickListener {
   public void onClick(View v);
}

This means if you say “implements OnClickListener” in your source code then you have to define a function called “onClick” that takes a View parameter and returns nothing.

So to summarize, you’re setting things up so that when the user clicks on a button, Android will call the onClick routine and pass it the button that was clicked on. Inside there you can check which button was clicked and take the appropriate action (starting a game, showing the About dialog, etc.).

6 posts, 3 voices