无法从单独的类中检索ArrayList

时间:2017-06-09 11:04:12

标签: java android arraylist

我有一个用于执行逻辑的DataHandaling类,它也被认为是从多个活动中检索ArrayList的一种更简单的方法,但是当我开始一个新的活动{ {1}}为空

注意:数组不为空,因为我在主要活动中多次使用相同的数组,这个问题只是我开始新活动时的情况)

来自ArrayList

的代码
DataHandaling

这是它在新活动中突破的地方

public class DataHandaling {
    EnviroClass enviroClass = new EnviroClass();
    private ArrayList<Event> eventArray = new ArrayList<Event>();

    public ArrayList<Event> getEventArray() {
        return eventArray;
    }

    public void getEvents() {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(enviroClass.url() + "/api/events/" + 7)
                .build();

        client.newCall(request)
                .enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        try {
                            JSONObject eventObject = new JSONObject(response.body().string());
                            JSONArray eventJsonArray = eventObject.getJSONArray("events");
                            for (int i = 0; i<eventJsonArray.length(); i++) {
                                eventObject = eventJsonArray.getJSONObject(i);
                                eventObject = eventObject.getJSONObject("event");
                                eventArray.add(new Event(eventObject.getString("name"), eventObject.getString("address"), eventObject.getString("image"), "100" ,eventObject.getString("start_date"), eventObject.getString("end_date"), eventObject.getInt("id")));
                            }
                            EventListAdapter adapter = new EventListAdapter(null, null);
                            adapter.swap(eventArray);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}

用于打开新活动的交互式监听器

textView.setText(dh.getEventArray().get(0).getName());

破坏的活动

@Override
public void onListFragmentInteraction(int id) {
    Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
    intent.putExtra("id", id);
    MainActivity.this.startActivity(intent);
}

堆栈追踪:

public class ProfileActivity extends AppCompatActivity {

    DataHandaling dh = new DataHandaling();

    int eventArrayId;

    TextView eventDescription;
    TextView eventName;
    ImageView eventImage;

    ArrayList<Event> eventArray = new ArrayList<Event>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        getSupportActionBar().setHomeButtonEnabled(true);

        Intent intent = getIntent();
        eventArrayId = intent.getIntExtra("id", 0);

        eventName = (TextView) findViewById(R.id.eventNameTextView);

        eventName.setText(dh.getEventArray().get(0).getName());

    }



}

3 个答案:

答案 0 :(得分:1)

您正在活动中创建DataHandling的新对象。因此,它不会与您之前使用的对象相同,并且其中包含数据。

答案 1 :(得分:0)

您的方法称为getArray(),但您正在调用getEventArray()。也许这就是原因。

编辑:既然你的消息是编辑的,我们就可以开展工作了。

DataHandaling dh = new DataHandaling();

这是一个新的DataHandaling对象,您的代码中永远不会更新他。所以我猜它的ArrayList默认是空的。

答案 2 :(得分:0)

首先: - 如果您的ArrayList<Event>为空,如果您获得第0个索引元素,那么在执行此行时您将获得NullPointerException: - dh.getEventArray().get(0).getName()因此将其替换为{{ 1}}会破坏。

因此,如果你想得到索引基础元素表单集合firsat checkl是否集合是空的。 像这样: -

null.getName()

第二: - 检查在 //if the ArrayList is not Empty then only iterate over it. if(!dh.getEventArray().isEmpty()) { textView.setText(dh.getEventArray().get(0).getName()); }

中添加事件的逻辑
相关问题