• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Arraylist Code Error

I have a problem with my code

package com.example.java;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ListView;

import org.w3c.dom.Element;

import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity3 extends AppCompatActivity {
private static final String Tag = "MainAcitivity3";
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Log.d(Tag, "onCreate: Started.");
ListView mListView = (ListView) findViewById(R.id.listView);
Intent intent = getIntent();
String asd = intent.getStringExtra(MainActivity2.namabarang);
String asdf = intent.getStringExtra(MainActivity2.jumlahstock);

Item sparepart = new Item(asd,asdf);
ArrayList[] itemlist = new ArrayList[1];
for(int i=0; i<2;i++)
{
itemlist = new ArrayList<Item>(); // initialize each element with ArrayList object
}
itemlist[0].add(sparepart);
itemlist[1].add(sparepart);

PersonListAdapter adapter = new PersonListAdapter(this, R.layout.adapter_view_layout,itemlist[0]);
mListView.setAdapter(adapter);

String number = intent.getStringExtra(MainActivity2.extraint);
if(number != null) {
FrameLayout lay = (FrameLayout) findViewById(R.id.frames);
if (number.equals("1")) {
lay.setVisibility(View.INVISIBLE);
mListView.setVisibility(View.VISIBLE);
} else {

}
}
else{}

}

public void onBtnClick (View view){
Intent intent = new Intent(this,MainActivity2.class);
startActivity(intent);
}



}

And i plan to save the item in specific array in the arraylist and here is the error i got

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.java/com.example.java.MainActivity3}: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones