p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional Android 4 Application Development (http://p2p.wrox.com/forumdisplay.php?f=685)
-   -   Earthquake chapter 7 part 2 (http://p2p.wrox.com/showthread.php?t=88470)

layingincouch September 18th, 2012 02:31 PM

Earthquake chapter 7 part 2
 
Hello,

Can you guys get this to work as according to the book? I mean: with API 4 and 15?

I got it work after aa lot of time and then i ended up with the support library and a FragmentActivity instead of an Activity in Earthquake.java. Which... isn't the point of the exercise?

I thought the point of this exercise was to get it to work without using a support library. Can anybody help me getting it to work according to today's standard.

Here's my full version of the class:
Code:

package com.paad.earthquake;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;

public class Earthquake extends FragmentActivity {
        static final private int MENU_PREFERENCES = Menu.FIRST + 1;
        static final private int MENU_UPDATE = Menu.FIRST + 2;
        private static final int SHOW_PREFERENCES = 1;

        public int minimumMagnitude = 0;
        public boolean autoUpdateChecked = false;
        public int updateFreq = 0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                updateFromPreferences();
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                super.onCreateOptionsMenu(menu);

                menu.add(0, MENU_PREFERENCES, Menu.NONE, R.string.menu_preferences);

                return true;
        }

        public boolean onOptionsItemSelected(MenuItem item) {
                super.onOptionsItemSelected(item);
                switch (item.getItemId()) {

                case MENU_PREFERENCES:
                        Class c = Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ? PreferencesActivity.class
                                        : FragmentPreferences.class;
                        Intent i = new Intent(this, c);

                        startActivityForResult(i, SHOW_PREFERENCES);
                        return true;

                }
                return false;
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == Activity.RESULT_OK)
                        updateFromPreferences();

                FragmentManager fm = getSupportFragmentManager();
                final EarthquakeListFragment earthquakeList = (EarthquakeListFragment) fm
                                .findFragmentById(R.id.EarthquakeListFragment);

                Thread t = new Thread(new Runnable() {

                        public void run() {
                                earthquakeList.refreshEarthquakes();
                        }
                });
                t.start();
        }

        private void updateFromPreferences() {
                Context context = getApplicationContext();
                SharedPreferences prefs = PreferenceManager
                                .getDefaultSharedPreferences(context);

                minimumMagnitude = Integer.parseInt(prefs.getString(
                                PreferencesActivity.PREF_MIN_MAG, "3"));
                updateFreq = Integer.parseInt(prefs.getString(
                                PreferencesActivity.PREF_UPDATE_FREQ, "60"));

                autoUpdateChecked = prefs.getBoolean(
                                PreferencesActivity.PREF_AUTO_UPDATE, false);
        }
}

Thanks in advance for any help/guidance with this problem!

plagoon October 31st, 2012 05:54 PM

When you say it doesn't work, what exactly do you mean? How far do you get? Is an exception being thrown? The version I just built works.


All times are GMT -4. The time now is 12:19 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.