Pilipe
Pilipe

Reputation: 134

How to retrieve variable datas with a class within a class. #Android

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.

Upvotes: 2

Views: 36

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93726

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.

Upvotes: 2

Related Questions