0

I'm just doing some learning and can't see why this ArrayList is initialised but can't be used outside of method its populated within. Can anyone explain? Thanks in advance...

For example if I aske the ArrayList.size() within the method it shows that it has been initialised but elsewhere outside the method it won't.

package com.purewowstudio.animations;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.GsonBuilder;
import com.purewowstudio.animations.extras.CustomGrid;
import com.purewowstudio.animations.models.ActorArray;
import com.purewowstudio.animations.models.ActorObject;

import java.util.ArrayList;

public class ActorList extends ActionBarActivity {

String LOG_TAG = ActorList.class.getSimpleName();
private ArrayList<ActorObject> actorObjectArrayList;
ActorArray actorArray;
GridView actorGrid;

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

    if (actorObjectArrayList != null) {
        ActorObject[] actors = actorObjectArrayList.toArray(new ActorObject[actorObjectArrayList.size()]);

        String actorsNames[] = new String[actorObjectArrayList.size()];
        String actorsImages[] = new String[actorObjectArrayList.size()];

        for (int i = 0; i < actorObjectArrayList.size(); i++) {
            actorsNames[i] = actors[i].getName();
            actorsImages[i] = actors[i].getImage();
        }

        CustomGrid adapter = new CustomGrid(ActorList.this, actorsNames, actorsImages);
        actorGrid = (GridView) findViewById(R.id.grid);
        actorGrid.setAdapter(adapter);
        actorGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                //Toast.makeText(ActorList.this, "You Clicked at " + actorsNames[+position], Toast.LENGTH_SHORT).show();
            }
        });
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.animation_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void getActors(){
    String url ="http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors";
    RequestQueue queue = Volley.newRequestQueue(this);

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    actorArray = new GsonBuilder().create().fromJson(response, ActorArray.class);
                    actorObjectArrayList = actorArray.getActors();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // TODO Auto-generated method stub
            Log.v(LOG_TAG, "Volley Error");
        }
    });
    queue.add(stringRequest);
}

}
8
  • 4
    If it's local to the method irs local to the method, that's just how Java works (most languages, I'd say). Commented May 12, 2015 at 13:21
  • your getActors method is asynchronous. By the time you call actorObjectArrayList != null, it is not yet initialized, because public void onResponse(String response) will be called some time in the future. Commented May 12, 2015 at 13:24
  • I thought that might have been the case so I took everything from onCreate and added it to it's own method, I then asked that method to start in 10 seconds, still didn't work? What's the que time like with Volley (First time I worked with it). I understand the server response will have a delay Commented May 12, 2015 at 13:30
  • Can you set a timeout after which, if there's no data in your data structure, you simply either call getActors again or stop the flow because the arraylist is empty? Commented May 12, 2015 at 13:34
  • 1
    I will try this now. Just for learning purposes, is the answer you provided below now suitable for this situation? Commented May 12, 2015 at 13:38

1 Answer 1

4

The reason why you can't access the content with which you initialized the arraylist in other methods is simple. It is local data inside the scope of the OnCreate. You should define a constructor of your class and initialize the arraylist there. Then in your methods, you can add the data that you need to add. When you'll be looking for data within the arraylist, it will have everything that you're looking for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.