[DD App] Day 3 – Fetching the post

Hi,

Today I did following updations to the app –

 

  1. Created another activity (PostDetailActivity) to be called using intent when we click on an individual post on our post list (recyclerview). We pass the slug derived from JSON data for that post as extra data to the intent.
  2. PostDetailActivity’s layout is a webview. The webview will fetch the post using the URL. The URL is formed using the slug passed as extra with the intent. We pass the slug as a string to the getUrl method of the NetworkUtils class.
  3. Added another method getUrl(String slug) inside the NetworkUtils class to create URL for fetching an individual post.
  4. Added another class (Post) to store attributes of a single post. It stores id, title, slug for an individual post.
  5. Each item of the RecyclerView was displayed as String before. But now each item is linked to a Post object having an id, title, and slug. And we have to pass the slug when we click on an item. Therefore we pass Post object instead of String Object to PostAdapterOnClickHandler’s onClick method. Changed String array to Post array for storing the posts received from the JSON.
  6. Similarly implementation of the onCLick method of PostAdapterOnClickHandler in the MainActivity changes. Now it starts an intent which sends a call to PostDetailActivity with a slug as extra data.
  7. The DDQueryTask returns a Post objects array instead of String array.
  8. We extract the post title from Post object using the getter methods to display them in the item’s TextView in PostAdapter class’s onBindViewHolder method.
  9. In DDJsonUtils, we extract the post id, title, and slug as store the data as Post object inside the Post array.

 

Files Created

 

  • Post.java [Store the attributes of a post like an id, title, slug]
  • PostDetailsActivity.java [Fetch the correct URL corresponding to given slug in received Intent and display it using webview.]
  • activity_post_details.xml [Display the webview]

 

Files Modified

 

  • MainActivity.java,
  • activity_main.xml
  • NetworkUtils.java
  • DDJsonUtils.java
  • DDQueryTask
  • AndroidManifest.xml

 

dd-app-day-3-fetching-post
First Screen – List of Posts
dd-app-day-3-fetching-post WebView displaying the fetched post
WebView displaying the fetched post

 

 

All the above work was done in 3 Pomodoros, ie, 1 hour 15 minutes.

You can follow the progress on this application development on Day 0, Day 1, Day 2.

That’s all for today, see you in the next post.

Thanks for stopping by!