0

Mans, im here around 2 hours in one thing it seems very simple but never return the numbers of objects.

I will explain whit code:

DownloadFileAsyncTask.java (is my Async Task)

in OnPostExecute i make this:

if(customAdapter != null){
    Log.i("solteiroApp","object count is "+customAdapter.getCount());
    ResultActivity.rec_count  = customAdapter.getCount(); // HERE I PASS COUNT
}

ResultActivity.java

public class ResultActivity extends Activity {

    public static int rec_count;
...

The problem is: When i get the ResultActivity.rec_count in onCreate always return 0, but i make the log how i show before:

Log.i("solteiroApp","object count is "+customAdapter.getCount());

and this return the count correctly.

I dont know because this, if someone have a sugestio to get this count please say me, im hours here try return this count to appear in the activity but nothing return the value in time of i execute, if i use again the AsyncTask return the previous value, please someone help me.

overallduka
  • 1,520
  • 19
  • 27

3 Answers3

0

1, is your getting rec_count in onCreate before OnPostExecute be invoked? check your code 2, if not, add volatile before rec_count: public static volatile int rec_count, please have a try, hope can help you.

Henry Gu
  • 51
  • 5
0

You can't do what you are trying to do. The Android OS will destroy and recreate static classes and their static variables when it (re)creates the Context they are to run in. I also learned (the hard way) that singleton classes can't be used to save data from one Activity to another.

What you need to do is either pass that data to the new Activity in its Intent Bundle, or save it in a SharedPreference. Using a callback interface for that is a bad idea, because you want the action to move forward to your next Activity and (if needed) allow the OS to destroy the previous Activity.

When creating the Intent for the next Activity, you use the Intent.putExtra(...) methods to add data to pass to the new Activity. In the new Activity, you use getIntent().get*Datatype*Extra(..) methods to retrieve the data.

Rick Falck
  • 1,778
  • 3
  • 15
  • 19
  • No is a transition to other activity, my AsyncTask return a list of objects and inside this i want get the count of this objects, thanks. – overallduka Nov 10 '13 at 12:14
0

I got it !

In my AsyncTask i pass how parameter the TextView and declare in AsyncTask how WeakReference, this:

public class DownloadFileAsyncTask extends AsyncTask<String,Void,String> {
..
    private WeakReference list=null,count_text=null; // Views i update in my AsyncTask
..

constructor:

public DownloadFileAsyncTask(View v,Context contextt,View txt) {
    list = new WeakReference(v);
    count_text = new WeakReference(txt);
    this.context = contextt;
}

and onPostExecute:

   ListView l = (ListView) list.get();
   TextView t = (TextView) count_text.get();

   t.setText(customAdapter.getCount()+" objects found");

Thanks all by the help, sometime is good leave sometime of the error and back, new ideas come back hehe, thanks all.

overallduka
  • 1,520
  • 19
  • 27