将 AsyncTask 与数据库一起使用 [英] Using AsyncTask with database

查看:25
本文介绍了将 AsyncTask 与数据库一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen_database); 
    db=new DBAdapter(this);         
    InitTask init_task=new InitTask();
    init_task.execute(db);
}

public class InitTask extends AsyncTask <DBAdapter, Void, List<GeoPoint>> {
    List<GeoPoint> geoPointsArray = new ArrayList<GeoPoint>();
    DBAdapter db;
    int latitude;
    int longitude;
    GeoPoint p;
    protected List<GeoPoint> doInBackground(DBAdapter...adapters) {
        try{
            db.openDataBase();
            Cursor c=db.getAllData();
            if (c.moveToFirst()) { 
                do{
                longitude=Integer.parseInt(c.getString(0));
                    latitude=Integer.parseInt(c.getString(1));
                    p = new GeoPoint(latitude,longitude);
                    geoPointsArray.add(p);
                } while(c.moveToNext());
            }
            c.close();
            db.close();
        } catch(Exception e) {
            Log.d("Eroare","doInBackground",e);
        }
        return geoPointsArray;
    }

    protected void onPostExecute(List<GeoPoint> geoPointsArray {
        super.onPostExecute(geoPointsArray);
        for (int i=0;i<geoPointsArray.size();i++) {
            Log.d("Lista",geoPointsArray.get(i).toString());
        }
    }
}

doInBackground 中,我尝试从数据库中读取数据,而在我的 onPostExecute() 中,我尝试显示数据,但我什么也没得到,而且我确信我在数据库中有一些东西.

In doInBackground I try to read from the database and in my onPostExecute() I try to display the data, but I get nothing, and I'm sure that I have something in the DB.

推荐答案

由于我没有看到你对 AsyncTask Params,Progress,Result 的声明,这很难调试,但我可以看到两个问题1) 您的 doInBackground 与原型 doInBackground(Params...) 不匹配2) 当线程 doInBackground 在 onPostExecute 中返回时,我看不到任何代码实际上对 geoPointsArray 执行任何操作.为了回答您的问题,在 onPostExecute 您又回到了 GUI 线程中.

Since I do not see your declaration for AsyncTask Params,Progress,Result this is difficult to debug, but I can see two problems 1) Your doInBackground does not match the prototype doInBackground(Params...) 2) I see no code that actually does anything with geoPointsArray when the threaded doInBackground returns in onPostExecute. To answer your question, at onPostExecute you are back in the GUI thread.

我有一些代码这里,这可能有帮助,也可能无济于事.

I have some code here, which may or may not help.

考虑将 db 作为参数传递

Consider passing db as the param

new AsynchTask<DataBase?,Void,List<GeoPoint>>

然后注释掉所有非数据库代码.将数据库代码包裹在 try catch Exception e 中.在异常时写入 Log.d(TAG,"doInBackground",e).然后在 onPostExecute 检查列表以查看 doInBackground 中的线程可能通过调用 Log.d(TAG,new Boolean(myList.isEmpty()).toString())

Then comment out all of the non database code. Wrap the database code in try catch Exception e. Write Log.d(TAG,"doInBackground",e) on exception. Then in onPostExecute examine the List to see what the thread in doInBackground is returning perhaps by calling Log.d(TAG,new Boolean(myList.isEmpty()).toString())

这篇关于将 AsyncTask 与数据库一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆