使用AsyncTask的与数据库 [英] Using AsyncTask with database

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

问题描述

我有以下的code:

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,进度,结果,这是难以调试,但我可以看到两个问题 1)您的doInBackground没有原型doInBackground(匹配参数...) 2)我没有看到code,其实做任何事情与geoPointsArray当线程doInBackground返回onPostExecute。要回答你的问题,在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.

我有一些code 这里,这可能会或可能不会帮助。

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

编辑:考虑通过DB作为参数

Consider passing db as the param

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

然后注释掉所有的非数据库code。 裹数据库code在尝试捕捉例外五。 在异常写Log.d(TAG,doInBackground,E)。 然后,在onPostExecute检查清单,看看有什么在doInBackground线程也许是通过调用Log.d返回(TAG,新布尔(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天全站免登陆