Skip to content

Commit

Permalink
feat: Implement LoaderCallbacks and display the data
Browse files Browse the repository at this point in the history
  • Loading branch information
divyanshupundir committed May 11, 2017
1 parent 636185a commit fe8f709
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/divyanshu/networking/EarthquakeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public List<Earthquake> loadInBackground() {
String requestURL = "https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-01-01&endtime=2016-05-02&minfelt=50&minmagnitude=5";
return NetworkHelper.fetchEarthquakeData(requestURL);
}

@Override
protected void onStartLoading() {
forceLoad();
}

}
42 changes: 41 additions & 1 deletion app/src/main/java/divyanshu/networking/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
package divyanshu.networking;

import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity {

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<Earthquake>>{

TextView tv_display_data;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv_display_data = (TextView) findViewById(R.id.tv_display_data);

getSupportLoaderManager().initLoader(0, null, this);
}


private void displayData(List<Earthquake> data) {
tv_display_data.setText("");

for (int i = 0; i < data.size(); ++i){
Earthquake earthquake = data.get(i);
tv_display_data.append(
earthquake.getMagnitude() + " - "
+ earthquake.getLocation() + " - "
+ earthquake.getTimeInMillis()
+ "\n"
);
}
}

@Override
public Loader<List<Earthquake>> onCreateLoader(int id, Bundle args) {
return new EarthquakeLoader(this);
}

@Override
public void onLoadFinished(Loader<List<Earthquake>> loader, List<Earthquake> data) {
displayData(data);
}


@Override
public void onLoaderReset(Loader<List<Earthquake>> loader) {
}
}
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
tools:context="divyanshu.networking.MainActivity">

<TextView
android:id="@+id/tv_display_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:text="Display Data"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down

0 comments on commit fe8f709

Please sign in to comment.