gpt4 book ai didi

java - ArrayList未初始化

转载 作者:行者123 更新时间:2023-12-01 18:14:59 25 4
gpt4 key购买 nike

我只是在做一些学习,不明白为什么这个 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);
}

}

最佳答案

无法访问其他方法初始化数组列表的内容的原因很简单。它是 OnCreate 范围内的本地数据。您应该定义类的构造函数并在那里初始化数组列表。然后在您的方法中,您可以添加需要添加的数据。当您在数组列表中查找数据时,它将包含您要查找的所有内容。

关于java - ArrayList未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30192080/

25 4 0