本文介绍了刷新数据Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个Android应用程序,使用Web服务带来了信息,那么当应用程序启动时我把数据存储在sqlite的,我管理一切从我的数据库。在应用程序启动后,数据填充一个ta​​blelayout。

I made an android application that brings information using a web service, then when the application starts I put the data in a sqlite and I manage everything from my database. After the application starts the data fills a tablelayout.

现在我想刷新内容每20秒(因为从Web服务的信息可以更改)。

Now I want to REFRESH that content every 20 seconds (Because information from webservice could changed).

我怎样才能做到这一点?我用的是onResume方法,但我不希望每次回到了tablelayout次刷新内容。

How can I do this? I used the onResume method, but I don't want to refresh content every time you went back to the tablelayout.

所以,我想要做的就是执行onCreate方法(这与Web服务连接,填补了我tablelayout并显示它),每20秒。我读到定时器或处理,但我不知道我怎样才能做到这一点。

So what I want to do is to execute the oncreate method (which connect with a webservice, fills my tablelayout and display it) every 20 seconds. I read about timer or handler but I'm not sure how can I do this.

现在我有一个问题!

我从网络服务的数据,并在我的doInBackground插入数据库中的数据......那好吧。现在,我创造一切textviews的tablerows等在onPostExecute方法,但我有2个问题。首先,

i get data from web service and insert data in database in my doInBackground.. thats ok. Now, i create all textviews, the tablerows, etc in the onPostExecute method, but i have 2 problems.First,

UsuariosSQLiteHelper usdbh =                            新UsuariosSQLiteHelper(这一点,DBIncidentes,NULL,1);

UsuariosSQLiteHelper usdbh = new UsuariosSQLiteHelper(this, "DBIncidentes", null, 1);

我有一个背景问题出在那里,在doInBackground方法

I have a context problem there, in the doInBackground method

和在onPostExecute方法,我有同样的问题,所有的本,如的TableRow rowTitulo =新的TableRow(本);

and in onPostExecute method, i have the same problem with all the "this" , like TableRow rowTitulo = new TableRow(this);

我知道这是一个方面的错误,我知道基本背景如何工作的,但我不知道如何解决这种情况下的问题。我认为,在异步初始化构造一个上下文可以帮助我替换onPOST等..请帮助!

i know this is a context error, i know basically how context works, but i dont know how to resolve this context problem. i thought that initializing a context in the async constructor may help and i replace in the onpost.. please help!

推荐答案

首先,你不希望从Web服务读取数据或将其写入到一个SQLite数据库中的onCreate方法。你需要生成一个新的线程,这样做,这样不会导致应用程序冻结。您可以创建一个线程或使用的AsyncTask 了解这一点。如果您使用的AsyncTask,那么你就可以覆盖其onPostExecute方法。这将在主UI线程上执行,这样你就可以刷新TableLayout这个方法。

First off you do not want to be reading data from a web service OR writing it to a SQLite database in your onCreate method. You need to spawn a new thread for doing this so that it does not cause your application to freeze up. You could create a Thread or use an AsyncTask for this. If you use an AsyncTask, then you can override its onPostExecute method. This will be executed on the main UI thread, so you can refresh your TableLayout in this method.

一旦你得到了这个code正常工作,那么你只需要安排它。要做到这一点最简单的方法是使用定时器的。下面是一些骨骼code,以帮助您开始:

Once you've got this code working properly, then you just need to schedule it. The easiest way to do this is using a Timer. Here is some skeleton code to help you get started:

class MyTask extends AsyncTask<Void,Void, MyDataStructure>{
    @Override
    protected MyDataStructure doInBackground(Void... params) {
        MyDataStructure data = new MyDataStructure();
        // get data from web service
        // insert data in database
        return data;
    }

    @Override
    protected void onPostExecute(MyDataStructure data) {
        TableLayout table = (TableLayout) findViewById(R.id.out);
        // refresh UI
    }
}

Timer timer = new Timer();
TimerTask task = new TimerTask(){

    @Override
    public void run() {
        new MyTask().execute();
    }

};
long whenToStart = 20*1000L; // 20 seconds
long howOften = 20*1000L; // 20 seconds
timer.scheduleAtFixedRate(task, whenToStart, howOften);

这篇关于刷新数据Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:29