gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:24:32 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

您可以使用观察者来做。

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);
            }
        });
    }

 
}


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

关于android - 如何在Xamarin.Android中创建实时数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53083378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com