-2

I bought the shittiest book "EVER" to learn android programming. It's the : Android App Development For Dummies, 3rd Edition

I assure you, it's not for "dummies"!

My question is: (This is a part of a widget code) remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent); 

As I type the code, android studio marks with red the word "widget". I checked in the R file but I couldn't find it. I checked in every file. Nowhere to find. The answer I get from Wiley customer support was:

Please reference http://developer.android.com/reference/android/widget/RemoteViews.html setOnClickPendingIntent(int viewId, PendingIntent pendingIntent) Equivalent to calling setOnClickListener(android.view.View.OnClickListener) to launch the provided PendingIntent. When setting the on-click action of items within collections (eg. ListView, StackView etc.), this method will not work. Instead, use {@link RemoteViews#setPendingIntentTemplate(int, PendingIntent) in conjunction with RemoteViews#setOnClickFillInIntent(int, Intent). Parameters viewId The id of the view that will trigger the PendingIntent when clicked pendingIntent The PendingIntent to send when user clicks .

A "dummy" doesn't understand all that things.

Is it a typo or did I did something wrong?

(I didn't write the whole code. If you want, I will happily do it)

Thank you in advance

user4938227
  • 579
  • 1
  • 5
  • 9

2 Answers2

1

I don't think the book is that bad, but I do understand your frustration. I've found numerous typos throughout the book and this instance is no different. The printed version in the book is

remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent);

but it should actually be this:

remoteViews.setOnClickPendingIntent(R.id.phone_state, pendingIntent);

phone_state is the id of the ImageView in your res/layout/app_widget.xml file.

sponge
  • 7,923
  • 13
  • 43
  • 77
  • Thank you very much for your time. I do believe it is a bad book for beginers. I don't recommend it. It is working the way you said. Thanks again. – user4938227 Jun 06 '15 at 14:19
  • You are quite welcome. They should certainly have spent more time editing the book. Particularly with programming languages, typos can mean all the difference between someone learning and someone putting it down forever. I don't know why the source code above wasn't just copied from the author's IDE. It would _never_ have worked as printed; which means for some reason he typed in the source manually. – HopeThisHelps Jun 06 '15 at 15:12
  • Upon further review, yes it is a pretty bad book. I can't get it to work as a widget. I even downloaded the author's source code (many differences from the book version by the way!) and it still won't run as a widget. I remember when DOS for Dummies came out--showing my age--and it was the greatest thing ever. They have REALLY lowered their standards. – HopeThisHelps Jun 06 '15 at 18:16
  • :-) I don't care about my 23€. The problem is I could have bought something helpful. And how is this possible this book in amazon to have 4 reviews with 5 stars? ("Spoiler alert") They have to advertise their book somehow. Nobody had run the code from then. And I thought I was the only one with this problem. And the references to the Android developers site .... I mean if I wanted to read from the site, I wouldn't have bought the book. – user4938227 Jun 06 '15 at 19:01
0

The R class is generated automatically from the resources you put into the res folder. For there to exist a field R.id.widget in your R class, you must have declared an element with android:id="@id/widget" somewhere in any of your layout files under res/layout or similar folders.

This would have been apparent if you had read any kind of introduction to Android programming, such as the official guide/tutorial. There's a page about resources and R at http://developer.android.com/guide/topics/resources/accessing-resources.html. I have no experience with the book you're referring to, but I'd be surprised if it calls itself an introduction to Android without mentioning the resource system, how to declare layouts in XML, and how to use them from code. I don't mean this in a derogatory way, but did you actually read the book, or just by any chance jumped straight into chapter 15 and expected to understand all details and concepts without, well, reading about them..?

JHH
  • 6,514
  • 5
  • 28
  • 64
  • Thanks for the response. I didn't declared an element with the name "widget". No. I didn't jumped into chapter 15. This app is the first in the book to write. – user4938227 May 25 '15 at 21:54