我只是在做一些学习,不知道为什么这个ArrayList被初始化但不能在其填充的方法之外使用。谁能解释一下?提前谢谢......
例如,如果我在方法中询问ArrayList.size(),它表明它已经初始化,但是在方法之外的其他位置它不会。
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);
}
}
答案 0 :(得分:4)
您无法访问在其他方法中初始化arraylist的内容的原因很简单。它是OnCreate范围内的本地数据。您应该定义类的构造函数并在那里初始化arraylist。然后在您的方法中,您可以添加需要添加的数据。当您在arraylist中寻找数据时,它将拥有您正在寻找的所有内容。