2013-02-25 53 views
9

編輯請參閱Observer Observables implementing issue - 看來我重寫了不需要的方法,而不是調用setChanged();在notify()之前;Observable/Observer Android

我一直在閱讀觀察者模式,以保持我的UI是最新的,但我仍然無法看到它的用途。即使在我的特定對象通知我的MainActivity然後運行update();方法我仍然無法使用Pet對象來拉取更新值,因爲對象是在Oncreate中創建的...而我只是無法創建一個新對象,因爲這樣變量將會不同。這就是我的實施,它似乎並沒有工作。因爲它不是在全球層面上定義

觀察員/ MainActivity

package com.grim.droidchi; 

import java.util.Observable; 
import java.util.Observer; 

import android.app.Activity; 
import android.app.AlarmManager; 
import android.app.PendingIntent; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.webkit.WebSettings.LayoutAlgorithm; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements Observer, OnClickListener { 
    private static final String TAG = "VPET"; 
    private static final String APP_PREFS = "VPET"; 
    private static final int REQUEST_CODE = 1; 
    Boolean isAlive = false; 
    TextView happiness_display, health_display, hunger_display, level_display; 
    Button PunchPet, UpdateHunger; 
    public static Pet pet = new Renamon(); 

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

     SharedPreferences settings = getSharedPreferences("APP_PREFS", 
       MODE_PRIVATE); 

     WebView myWebView = (WebView) findViewById(R.id.pet_display); 
     myWebView.loadUrl("file:///android_asset/renamon.gif"); 
     myWebView.setInitialScale(10000); 
     myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); 


     PunchPet = (Button) findViewById(R.id.PunchPet); 
     UpdateHunger = (Button) findViewById(R.id.UpdateHunger); 
     final TextView hunger_display = (TextView) findViewById(R.id.hunger_display); 
     TextView happiness_display = (TextView) findViewById(R.id.happiness_display); 
     TextView level_display = (TextView) findViewById(R.id.level_display); 
     TextView health_display = (TextView) findViewById(R.id.health_display); 

     hunger_display.setText(Integer.toString(pet.getHunger())); 
     health_display.setText(Integer.toString(pet.getHP())); 
     level_display.setText(Integer.toString(pet.getLVL())); 
     happiness_display.setText(Integer.toString(pet.getHappy())); 


     Intent intent = new Intent(this, Gameloop.class); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(
       getBaseContext(), REQUEST_CODE, intent, 0); 

     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis() + (5 * 1000), 1800000, pendingIntent); 
     // 1800000 ms = 30 mins 

     pet.feed(); 
     pet.addObserver(this); 

    } 

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

    @Override 
    protected void onPause() { 


     super.onPause(); 
    } 

    @Override 
    public void update(Observable o, Object data) { 

     hunger_display.setText(Integer.toString(pet.getHunger())); 
     health_display.setText(Integer.toString(pet.getHP())); 
     level_display.setText(Integer.toString(pet.getLVL())); 
     happiness_display.setText(Integer.toString(pet.getHappy())); 
     Log.d(TAG, "UPDATED FROM OBSERVER"); 

    } 

    @Override 
    public void onClick(View v) { 
     if (v == PunchPet) { 
      pet.setHP(500); 
      Toast.makeText(getApplicationContext(), "PUNCHPET", Toast.LENGTH_SHORT).show(); 
      health_display.setText(Integer.toString(pet.getHP())); 
    }else { 

    } 



    } 

} 

可觀察/寵物

package com.grim.droidchi; 

import java.util.Observable; 
import java.util.Observer; 
import java.util.Set; 

import android.util.Log; 

public class Pet extends Observable implements PetInterface { 
    protected Set<Observer> observers; 
    private static final String TAG = "VPET"; 
    private int Health = 100; 
    @Override 
    public void addObserver(Observer o) { 
     observers.add(o); 
     super.addObserver(o); 
    } 

    @Override 
    public void notifyObservers() { 
     observers.notify(); 
     super.notifyObservers(); 
    } 

    @Override 
    public synchronized void deleteObserver(Observer o) { 
     observers.remove(o); 
     super.deleteObserver(o); 
    } 

    private int Happiness = 10; 
    private int Level = 1; 
    private int Hunger = 0; 
    private int Exp = 0; 
    private String Name; 
    private Boolean isAlive = true; 
    private Boolean isSick = false; 

    public void setHP(int hp) { 
     this.Health = hp; 
     notifyObservers(hp); 
    } 

    public void setLVL(int lvl) { 
     this.Level = lvl; 
     notifyObservers(lvl); 
    } 

    public void setXP(int xp) { 
     this.Exp = xp; 
     notifyObservers(xp); 
    } 

    public void setHunger(int hunger) { 
     this.Hunger = hunger; 
     notifyObservers(hunger); 
    } 

    public void setHappy(int happy) { 
     this.Happiness = happy; 
     notifyObservers(happy); 
    } 

    public int getHP() { 

     return Health; 
    } 

    public int getLVL() { 

     return Level; 
    } 

    public int getXP() { 

     return Exp; 
    } 

    public int getHunger() { 

     return Hunger; 
    } 

    public int getHappy() { 

     return Happiness; 
    } 

    public boolean isAlive() { 
     return isAlive; 

    } 

    public boolean isSick() { 
     return isSick; 

    } 

    @Override 
    public void sleep() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void clean() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void feed() { 
     Log.d(TAG, "FEEDING FROM INTERFACE THING"); 

    } 

    @Override 
    public void passtime() { 

    } 

} 
+2

我想你應該看看該update方法的引用:http://www.youtube.com/watch? v = wiQdrH2YpT4 – 2013-02-25 14:13:35

回答

3

首先,我不建議在您的可觀察對象中重寫addObserverremoveObserver。這個API在實現這個方面做得很好(除非你想要一些特定的功能)。

其次,方法notifyObservers()被重載,以便您可以將它傳遞給一個對象,即IE notifyObservers(Object obj)。如果您通過了Pet,notifyObservers(this),那麼您將會看到正在觀察的對象Pet

我認爲這是你的問題,但請糾正我,如果我錯了。

編輯:爲了澄清,我的答案取決於您在Java API here中使用Observable類。

經進一步審查,你應該已經到Pet對象調用的Observer

+1

看到多數民衆贊成我遇到的問題,當我以前提出的問題,我剛剛有例如對我拋出的代碼。我被告知我需要實現一個我自己的Observable接口,我不明白爲什麼,因爲我知道我已經寫了文件,我知道它什麼都不做......這是我可觀察的接口.. 'package com .grim.droidchi; import java.util.Observer; public interface Observable {0} {0};}}}}}}} public void addObserver(Observer o); \t public void removeObserver(Observer o); \t public void notifyObservers(Observer o); }' – Pheonix2105 2013-02-25 14:21:33

+0

java API有一個可觀察的類位於:http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Observable.html。我會強烈建議擴展這個類,而不是編寫你自己的家庭解決方案。我意識到其他人可能會不同意這種方法,因爲它需要你傳遞Objects和'instanceof',但我認爲它可以滿足你的要求。 – nattyddubbs 2013-02-25 14:23:32

+0

沒有Observable可用於擴展,我必須創建我自己的接口,或者它只是說「 Observable無法解析爲類型「 – Pheonix2105 2013-02-25 14:49:25

1

pet沒有其他方法訪問。

您應該在onCreate方法之外定義Pet pet = new Renamon();