Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Adapting Data for Display in a List Challenge 1 of 2

I'm having trouble on this one. Can anyone help me? Add the 'onPostExecute()' method to the AsyncTask below. Make it 'protected void' and remember that the parameter is related to the return type of 'doInBackground()'.

Here's what I have: import android.os.AsyncTask; import android.os.Bundle;

public class CustomActivity extends Activity {

public String mStatus;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom);
    CustomAsyncTask task = new CustomAsyncTask();
    task.execute();

}

 protected void updateStatusLabel() {
    super.updateStatusLabel();
}

protected class CustomAsyncTask extends AsyncTask<Object, Void, String> {   


    @Override
    protected String doInBackground(Object... params) {
        // Imagine this is some code to do some work
        return "Done!";
  protected void onPostExecute(JSONString result) {
    doInBackground = result;


  }
    }


}

}

@Override protected void onPostExecute(String result) { mStatus= result; }

I believe the error is at the onPostExecute method. doInBackground method will return the String "Done!" to the onPostExecute input String result. You can then set them Status String variable in the CustomActivity class with the input String result.

Hope I am clear. Cheers.

2 Answers

Hi Milan

The comment by Peh Hui Shi is half there, you do need to define the paramater you are passing into the onPostExecute as a String data type.

You also have some other errors though, the onPostExecute method is currently inside of the doInBackground method which is incorrect as once the code sees the return statement it will exit out of the current method and the code you added will never be run. Secondly you missed out the @override annotation that must be included with the onPostExecute method.

I have attached the code below,

Hope this helps

Daniel

protected class CustomAsyncTask extends AsyncTask<Object, Void, String> {   
    @Override
    protected String doInBackground(Object... params) {
        // Imagine this is some code to do some work
        return "Done!";
    }
    @Override
    protected void onPostExecute(String result){
    //Add the code in to this method for the second challenge      
    }

}

Thanks again Daniel! That was a huge help!

Not a problem, glad I could help!

Thank Daniel for pointing out!

It's fun we can learn and interact with friends from all over the world at the same time.

I'm also having a bit of trouble on this challenge. Any suggestions?

Some YouTube data in the file 'data.json' has been loaded into a JSONObject named 'jsonData'. Using 'data.json' as your guide, write a 'for' loop that loops through 'jsonVideos'. In each step of the loop, store the value of the 'title' property in the 'titles' array.

for (i = 0; i < jsonVideos.length(); i ++) { JSONObject video = jsonVideos.getObject(i); String[] title = video.getString("title"); }

Hi Milan

This particular challenge seems to be a sticking point for quite a few people, the coverage of arrays is too little in my opinion.

your for loop is correct, only you are not using the correct method call try getJSONObject not getObject. Also when putting something into an array you must give the position otherwise JAVA will not know where to put it. so you use the line titles[i] = video.getString("title");

for (int i = 0; i<jsonVideos.length();i++){
  JSONObject video = jsonVideos.getJSONObject(i);
  titles[i] = video.getString("title");
}

Hope this helps

P.S if you have any further questions please post a new question and ask me that way, I know it might seem a bit sad but I do like to get the points if possible! Cheers

Daniel

Thanks again Daniel! For some reason, that was the one option I ruled out for some crazy reason. Ugh!

public class CustomActivity extends Activity {

public String mStatus;

protected class CustomAsyncTask extends AsyncTask<Object, Void, String> {

@Override
protected String doInBackground(Object... params) {
    // Imagine this is some code to do some work
    return "Done!";
}

@Override
protected void onPostExecute(String result) {
   //Add the code in to this method for the second challenge
   //Remember to pass the result back to the main UI thread     
   mStatus= result;  
}

}

}

Hope this help. Cheers!

Thank you!