Android Open Source - EarthquakeAPIViewer Main Activity

From Project

Back to project page EarthquakeAPIViewer.


The source code is released under:

Apache License

If you think the Android project EarthquakeAPIViewer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.earthquakeapiviewer;
//www .  j  a  va 2 s.  c  o  m
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

  ListView _listView;
  CallApi _callApi;
  EarthquakeAdapter _earthquakeAdapter;
    ArrayList<Earthquake> _list;
    protected void onCreate(Bundle savedInstanceState) {

        _listView = (ListView) findViewById(;
        _callApi = new CallApi(this);
        _list = new ArrayList<Earthquake>();
        _earthquakeAdapter = new EarthquakeAdapter(this, _list);

        _listView.setOnItemClickListener(new OnItemClickListener() {

      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Earthquake eq = (Earthquake)parent.getItemAtPosition(position);

        Intent intent = new Intent()
          .setClass(getBaseContext(), DisplayEarthquakeActivity.class)
             .putExtra("Earthquake", eq);

    public void updateData(ArrayList<Earthquake> data) {
        runOnUiThread(new Runnable() {
            public void run() {


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    public boolean onOptionsItemSelected(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.
        int id = item.getItemId();
        if (id == {
          _callApi = new CallApi(this);
            return true;
        return super.onOptionsItemSelected(item);
  private class EarthquakeAdapter extends ArrayAdapter<Earthquake> {

      private final Context context;
      private final ArrayList<Earthquake> values;

      public EarthquakeAdapter(Context context, ArrayList<Earthquake> values) {
        super(context, android.R.layout.simple_list_item_1, values);
          //R.layout.earthquake_row, values);
        this.context = context;
        this.values = values;
      public EarthquakeAdapter(Context context, int resource, int textViewResourceId, ArrayList<Earthquake> values) {
        super(context, resource, textViewResourceId, values);
        this.context = context;
        this.values = values;
    public View getView(int position, View convertView, ViewGroup parent) {
      LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.earthquake_row, parent, false);
        TextView tv1 = (TextView) rowView.findViewById(;
        String text1;
          text1 = "Location";
          //text1 = String.format("Latitude: %s Longitude: %s",this.values.get(position).latitude, this.values.get(position).longitude);
          //text1 = getAddress(this.values.get(position).address, position);//, this.values.get(position).longitude);///String.format("Latitude: %s Longitude: %s", this.values.get(position).latitude, this.values.get(position).longitude);
        text1 = this.values.get(position).address;

        TextView tv2 = (TextView) rowView.findViewById(;
        String magnitude = String.format("Magnitude: %s", this.values.get(position).magnitude);
        float mag = Float.parseFloat(this.values.get(position).magnitude);
        if(mag >= 8)
          rowView.setBackgroundColor(Color.rgb(227, 77, 48));
        else if(mag >= 5 && mag < 8)
          rowView.setBackgroundColor(Color.rgb(227, 143, 48));
        else rowView.setBackgroundColor(Color.rgb(227, 206, 48));
        return rowView;

Java Source Code List