简体   繁体   English

Android 使用数据库值更新应用程序视图

[英]Android App update view with value from database

So I am currently in the early stages of writing an Android app in Java, using Android Studio.因此,我目前正处于使用 Android Studio 在 Java 中编写 Android 应用程序的早期阶段。 I have the MainActivity, which consists of a TextView and a Button.我有 MainActivity,它由一个 TextView 和一个按钮组成。

The idea is that when I press the button, an entity-objekt is fetched from a Room-Database, a String-Member is extracted from the object and then used to set the new text for the TextView.这个想法是,当我按下按钮时,从房间数据库中获取一个实体对象,从 object 中提取一个字符串成员,然后用于为 TextView 设置新文本。 I would preferably do this in such a way that by pressing the button a new object is fetched from the database and the TextView is updated, all without having to start a new Activity.我最好通过按下按钮从数据库中获取新的 object 并更新 TextView 来执行此操作,而无需启动新的活动。

The onClick-Method for the button looked something like this in its first version:按钮的 onClick 方法在其第一个版本中看起来像这样:

private void getFoo() {
    Foo foo = db.fooDao().getRandom();
    String fooName = foo.getFooName();
    textFoo.setText(fooName);
}

The first problem was that I tried fetching the object from database as is, in the main thread, which the compiler was not happy about.第一个问题是我尝试在主线程中按原样从数据库中获取 object,编译器对此并不满意。 Tried to wrap the database interaction into a seperate thread, but then I either had to figure out how to make the value fetched inside the thread to be available outside of it OR I had to nest a call to runOnUiThread in my Thread run() method.试图将数据库交互包装到一个单独的线程中,但是我要么必须弄清楚如何使线程内部获取的值在线程外部可用,要么我必须在我的 Thread run() 方法中嵌套对 runOnUiThread 的调用.

I've had mixed results, with one configuration you could see the new text be displayed on screen, and a split-second after that the app crashed.我的结果好坏参半,通过一种配置,您可以看到新文本显示在屏幕上,然后一秒钟后应用程序崩溃了。

Can I make this work as an update of my view within the same Activity or should I instead just start a new one?我可以在同一个活动中更新我的视图,还是应该开始一个新的?

Okay I have overlooked a different SO question that perfectly solved my problem.好的,我忽略了一个完全解决我的问题的不同的 SO问题。 My code for my getFoo now basically looks like this and fulfils the intended function:我的 getFoo 代码现在基本上看起来像这样并满足预期的 function:

private void getFoo() {
    class LoadFooTask extends AsyncTask<Void, Void, Foo> {
        @Override
        protected Foo doInBackground(Void... voids) {
            return db.fooDao().getRandom();
        }

        @Override
        protected void onPostExecute(Foo foo) {
            String FooName = foo.getFooName();
            textFoo.setText(fooName);
        }
    }
    LoadDishTask loadDishTask = new LoadDishTask();
    loadDishTask.execute();
}

The asynchronous task of loading from the database is done inside the doInBackground() method and the UI update is done on the main thread via the onPostExecute() method.从数据库加载的异步任务在 doInBackground() 方法内部完成,UI 更新通过 onPostExecute() 方法在主线程上完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM