1

I have the following 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());
        }
    }
}

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.

Gaffi
  • 4,307
  • 8
  • 43
  • 73
adrian
  • 4,574
  • 17
  • 68
  • 119
  • 1
    from your updated code change db.openDataBase() to adapters[0].openDataBase() then remove super.onPostExecute then fix curly braces in for or use the boolean code I posted by checking if geoPointsArray is null and if not null calling isEmpty() to see if it is empty – JAL May 08 '11 at 01:03
  • It works...in onPostExecute I have all the data....with super.onPostExecute() I render this to UI thread???Thank uuu – adrian May 08 '11 at 10:28
  • The trick is to declare the InitTask class inside of the UI activity class. Then you can touch the UI variables of the activity inside the method onPostExecute! To understand how this works look up nested classes or inner classes OR JUST TRY IT Have fun. – JAL May 08 '11 at 19:57

2 Answers2

1

Firstly, you haven't declared your AsyncTask class correctly, as can be seen from the example in android documentation here you need to specifify the three types it will use:

public class InitTask extends AsyncTask<Params, Progress, Result>

This will fix your doInBackround problem. The reason it isn't recognised is Result needs to be substituted for your List.

To get the data back to the UI thread, you need to set up a listener, onProgressUpdate() which will get called, passing the Progress object which you send from the AsyncTask using publishProgress().

Jodes
  • 14,118
  • 26
  • 97
  • 156
1

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.

EDIT: Consider passing db as the param

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

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())

JAL
  • 3,319
  • 2
  • 20
  • 17
  • I had my parameters but I don't know why it didn't displayed them...anyway I still get force close..... – adrian May 07 '11 at 23:34
  • And if u are here there is another thing-do u know how could I render the data I have in geoPointsArray...step by step????I mean once the doInBackground is complete in geoPointsArray I'll have all the data which I'll pass it to RouteDraw which will draw a line between them.What I want is something like rendering a point-draw the line,render another point and draw the line and so on....Any clue of how could I do that?Thx – adrian May 08 '11 at 00:03
  • @adrian... Once you solve the problem of getting the data. I would just post a separate question. I am definitely NOT one to query about graphics questions! – JAL May 08 '11 at 00:13
  • U are the only one who answers:)))......and the question is about how to get out the data step by step;)...thx – adrian May 08 '11 at 00:16