Skip to content Skip to sidebar Skip to footer

Android Head First "nasa Daily Image App"

I've been following the book 'Head First Android', and I'm stuck in Chapter 3. This little app is a really basic layout; 3 text views and 1 image view, that should update after rea

Solution 1:

You should use AsyncTask as an inner class.

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    IotdHandlerhandler=newIotdHandler ();
    newMyTask().execute();
}

and then parse document in doInBackground() and call resetDisplay in onPostExecute().

publicclassMyTaskextendsAsyncTask<Void, Void, Void>{

@OverrideprotectedVoiddoInBackground(Void... params) {
    handler.processFeed();
    returnnull;
}

@OverrideprotectedvoidonPostExecute(Void result) {
    resetDisplay (handler.getTitle(), handler.getDate(), handler.getImage(), handler.getDescription());
    super.onPostExecute(result);
}
}

For more info how to pass parameter,return result etc.. AsyncTask Document

Solution 2:

There will be more errors you will be facing if you go ahead from this book and that's why head first has took this book from the amazon.So if u are beginners then try Boston, Marcana ,vogella tutorils or u can try Android tutorials from google and the same question you can refer to Recommend a good Android tutorial with Step by Step examples

Solution 3:

One of the other problems with this is that the RSS feed as of the time of the writing of the book was a single post, and now it is a list of the most recent thirty days of the Daily image feed. This brought me to the same issue. So updating the XML processing and using the AsyncTask was required to get a working app.

Solution 4:

The two clases solved:

1 - IotdHandler:

package com.headfirstlab.nasadailyimage;

     import android.support.v7.app.ActionBarActivity;
     import android.os.Bundle;
     import android.view.Menu;
     import android.view.MenuItem;
     import android.widget.ImageView;
     import android.widget.TextView;

    publicclassNasaDailyImageextendsActionBarActivity {

    IotdHandleriotdHandler=newIotdHandler();

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nasa_daily_image);

    IotdHandlerhandler=newIotdHandler();

    handler.processFeed();
    resetDisplay(iotdHandler.getTitle(), iotdHandler.getDate(),
            iotdHandler.getUrl(), iotdHandler.getDescription());
}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.nasa_daily_image, menu);
    returntrue;
}

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.intid= item.getItemId();
    if (id == R.id.action_settings) {
        returntrue;
    }
    returnsuper.onOptionsItemSelected(item);
}

privatevoidresetDisplay(String title, String date,
        String imageUrl, String description) {

    TextViewtitleView= (TextView)findViewById(R.id.imageTitle);

    titleView.setText(title);

    TextViewdateView= (TextView)findViewById(R.id.imageDate);

    dateView.setText(date);

    ImageViewimageView=(ImageView)findViewById(R.id.imageDisplay);

    imageView.setImageBitmap(iotdHandler.getUrl());
    imageView.setImageBitmap(IotdHandler.getBitmap(iotdHandler.getUrl()));
    TextViewdescriptionView= (TextView)findViewById(R.id.imageDescription);

    descriptionView.setText(description);


    }
 }

2 - NasaDailyImage:

package com.headfirstlab.nasadailyimage;

 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.ImageView;
 import android.widget.TextView;

 publicclassNasaDailyImageextendsActionBarActivity {

 IotdHandleriotdHandler=newIotdHandler();

   @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nasa_daily_image);

    IotdHandlerhandler=newIotdHandler();

    handler.processFeed();
    resetDisplay(iotdHandler.getTitle(), iotdHandler.getDate(),
            iotdHandler.getUrl(), iotdHandler.getDescription());
}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.nasa_daily_image, menu);
    returntrue;
}

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.intid= item.getItemId();
    if (id == R.id.action_settings) {
        returntrue;
    }
    returnsuper.onOptionsItemSelected(item);
}

privatevoidresetDisplay(String title, String date,
        String imageUrl, String description) {

    TextViewtitleView= (TextView)findViewById(R.id.imageTitle);

    titleView.setText(title);

    TextViewdateView= (TextView)findViewById(R.id.imageDate);

    dateView.setText(date);

    ImageViewimageView=(ImageView)findViewById(R.id.imageDisplay);

    imageView.setImageBitmap(IotdHandler.getBitmap(iotdHandler.getUrl()));
    TextViewdescriptionView= (TextView)findViewById(R.id.imageDescription);

    descriptionView.setText(description);


     }
}

hope you find it useful : )

Post a Comment for "Android Head First "nasa Daily Image App""