如何在Xamarin.Android中创建实时数据?

时间:2018-10-31 12:30:31

标签: android xamarin xamarin.android android-livedata

我正在创建一个示例应用程序来研究MVVM android软件体系结构,但是我无法在xamarin.android文档中找到有关此指南。

我创建了数据库及其[ROOM]的方法,然后创建了实体。

然后,由于我正在关注android教程(因为找不到xamarin.android一个),因此创建实时数据时遇到了问题。

所以我的问题是,有没有一种方法可以在xamarin.android中创建实时数据,如果有,我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用Observer进行操作。

MyPerson 是可观察的

    public class MyPerson extends Observable {
 
    private int age;
    private String name;
    private String sax;
     
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
        setChanged();
        notifyObservers();
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
        setChanged();
        notifyObservers();
    }
 
    public String getSax() {
        return sax;
    }
 
    public void setSax(String sax) {
        this.sax = sax;
    }
 
    @Override
    public String toString() {
        return "MyPerson [age=" + age + ", name=" + name + ", sax=" + sax + "]";
    }
}

MyObserver 是观察者

public class MyObserver implements IObserver {
     
    private int i;
    private MyPerson myPerson;//object be observered
     
    public MyObserver(int i){
        System.out.println("I am observer---->" + i);
        this.i = i;
    }
 
    public int getI() {
        return i;
    }
 
    public void setI(int i) {
        this.i = i;
    }
 
    public MyPerson getMyPerson() {
        return myPerson;
    }
 
    public void setMyPerson(MyPerson myPerson) {
        this.myPerson = myPerson;
    }
 
    @Override
    public void update(Observable observable, Object data) {
        System.out.println("Observer---->"+ i +"Get Update!");
        this.myPerson = (MyPerson)observable;
        System.out.println(((MyPerson)observable).toString());
    }
 
}
您使用

MainActivity 进行测试

     
    private Handler handler = new Handler(new Handler.Callback() {
         
        @Override
        public boolean handleMessage(Message msg) {
            // add info into list to show 
            MyListAdapter myListAdapter = new MyListAdapter(MainActivity.this, myObservers);
            lv.setAdapter(myListAdapter);
            return false;
        }
    });
 
    @Override
    protected override void OnCreate(Bundle savedInstanceState){
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
        observable = new MyPerson();
        myObservers = new ArrayList<MyObserver>();
        lv = getListView();
        //add observer
        add.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                MyObserver myObserver = new MyObserver(i);
                i++;
                observable.addObserver(myObserver);
                myObservers.add(myObserver);
                handler.sendEmptyMessage(0);
            }
        });
        change = (Button) findViewById(R.id.change);
        //notify data change
        change.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                observable.setAge(10 + i);
                observable.setName("a" + i);
                observable.setSax("man" + i);
                handler.sendEmptyMessage(0);
            }
        });
    }

 
}

添加此按钮以添加观察者,更改此按钮以测试更改的数据。某些代码应更改为表单样式。