10

编辑请参阅Observer Observables 实施问题- 似乎我正在覆盖不需要的方法并且没有调用 setChanged(); 在通知()之前;

我一直在阅读观察者模式以使我的 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() {

    }

}
4

2 回答 2

4

首先,我不建议覆盖addObserverremoveObserver在您的可观察对象中。API 在实现这一点方面做得非常好(除非你想要一些特定的功能)。

其次,该方法notifyObservers()被重载,因此您可以向它传递一个对象 IE notifyObservers(Object obj)。如果您将 , 传递给它PetnotifyObservers(this)那么您将获得对Pet正在观察的对象的引用。

我认为这是您的问题,但如果我错了,请纠正我。

编辑:澄清一下,我的回答取决于您在此处Observable使用Java API 中的类。

经过进一步审查,您应该已经拥有对Pet调用update方法的对象的引用Observer

于 2013-02-25T14:16:55.603 回答
1

pet无法在其他方法中访问,因为它未在全局级别定义。

您应该在onCreate方法Pet pet = new Renamon();之外定义。

于 2013-02-25T14:15:14.933 回答