Skip to content

Instantly share code, notes, and snippets.

@jmarkman
Created April 9, 2018 01:40
Show Gist options
  • Save jmarkman/5026b59f8af94b7cd9b19e4d4ba6c396 to your computer and use it in GitHub Desktop.
Save jmarkman/5026b59f8af94b7cd9b19e4d4ba6c396 to your computer and use it in GitHub Desktop.
Front Page activity that contains my RecyclerView
public class FrontPage extends AppCompatActivity
{
private ProgressBar loadingProgress;
private RecyclerView rvArticles;
private EndlessRecyclerViewScrollListener scrollListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_page);
rvArticles = findViewById(R.id.rv_front_page_articles);
loadingProgress = findViewById(R.id.front_page_progress);
try
{
URL topStories = HackerNewsAPI.buildTopStoriesURL();
new ArticleQueryTask().execute(topStories);
}
catch (Exception ex)
{
Log.e("onCreate Error","Something happened");
ex.printStackTrace();
}
LinearLayoutManager articlesLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
rvArticles.setLayoutManager(articlesLayoutManager);
scrollListener = new EndlessRecyclerViewScrollListener(articlesLayoutManager)
{
@Override
public void onLoadMore(int page, int totalItemsCount, RecyclerView view)
{
loadMoreArticles(page);
}
};
rvArticles.addOnScrollListener(scrollListener);
}
// This is where the dynamic loading magic should occur
private void loadMoreArticles(int offset)
{
}
public class ArticleQueryTask extends AsyncTask<URL, Void, ArrayList<String>>
{
@Override
protected ArrayList<String> doInBackground(URL... urls) {
URL searchURL = urls[0];
String articleIDJSON = null; // variable for storing article ID JSON
ArrayList<String> result = new ArrayList<>(); // variable for storing story response JSON
try
{
articleIDJSON = HackerNewsAPI.getJSON(searchURL);
// Store the article IDs in an ArrayList to turn them into URLs for API requests
ArrayList<String> articleIDs = HackerNewsAPI.getStoryIDsFromJSON(articleIDJSON);
// This processes EVERY article in the returned ArrayList
// for (String url : articleIDs)
// {
// URL articleURL = HackerNewsAPI.buildStoryURL(url);
// String jsonResult = HackerNewsAPI.getJSON(articleURL);
// result.add(jsonResult);
// }
// This just does the top 10
for (int i = 0; i <= 9; i++)
{
URL articleURL = HackerNewsAPI.buildStoryURL(articleIDs.get(i));
String jsonResult = HackerNewsAPI.getJSON(articleURL);
result.add(jsonResult);
}
}
catch (IOException ex)
{
Log.e("ArticleQuery Error", "JSON error in doInBackground");
}
return result;
}
@Override
protected void onPostExecute(ArrayList<String> result)
{
TextView tvError = findViewById(R.id.front_page_error);
// Make sure that the progress bar is invisible once all tasks are done
loadingProgress.setVisibility(View.INVISIBLE);
// Logic for if no results are returned; if no results, display an error
if (result == null || result.size() == 0)
{
rvArticles.setVisibility(View.INVISIBLE);
tvError.setVisibility(View.VISIBLE);
}
else
{
rvArticles.setVisibility(View.VISIBLE);
tvError.setVisibility(View.INVISIBLE);
}
// Get the article data from the JSON, put the data for each article in
// an article object, and then put that object in an ArrayList of Articles
// Then, use that ArrayList as the source for our ArticleAdapter, and
// finally set the RecyclerView to use that adapter
ArrayList<Article> articles = HackerNewsAPI.getArticlesFromJSON(result);
ArticleAdapter adapter = new ArticleAdapter(articles);
rvArticles.setAdapter(adapter);
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
// Display the progress bar during execution
loadingProgress.setVisibility(View.VISIBLE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment