2

I have a class in a class like this:

public class MainActivity extends ListActivity {

    ArrayList<HashMap<String, String>> arrayInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.connection_beging);

        arrayInfo = new ArrayList<HashMap<String, String>>();

        ...

        button_connection.setOnClickListener(new View.OnClickListener(){
        public void onClick(View R){
                new GetInfoConnexion().execute();
                arrayInfo.get(0).get(TAG_FIRSTNAME); // Not working because arrayInfo is null
            }
        }
    }

    private class GetInfoConnexion extends AsyncTask<Void, Void, Void> {

         protected Void doInBackground(Void... arg0) {
              ...
              arrayInfo.add(info); //all datas are placed into arrayInfo. 
              // This works perfectly.
              arrayInfo.get(0).get(TAG_FIRSTNAME); // example work ! 
         }
    }
}

How retrieve the datas placed in arrayInfo for use in class MainActivity ? (more precisely in onCLick() after new GetInfoConnexion().execute();).

Thank you in advance.

3
  • Um, ArrayInfo is a member of MainActivity, so it just has to access it. Be careful though- if you access it on the AsyncTask and in the UI thread you risk race conditions unless you put some form of synchronizaton in there. Jun 29, 2014 at 1:09
  • 2
    Thanks but how do i proceed ?
    – Pilipe
    Jun 29, 2014 at 1:18
  • Are you sure arrayInfo is null? Maybe arrayInfo.get(0) is null.
    – user253751
    Jun 29, 2014 at 1:21

1 Answer 1

2

Ah, ok, I get what you're asking now. You don't access it in onClick. Add a function named onPostExecute to your AsyncTask, and place the code you want to use in there. The reason you can't do it in onClick is because the AsyncTask runs in parallel and it isn't done yet. It will call onPostExecute when its finished.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.