1. Are you ready for the Galaxy S20? Here is everything we know so far!

Arraylist Code Error

Discussion in 'Android Development' started by Daniel Tianussan, Aug 31, 2021.

  1. Daniel Tianussan

    Thread Starter

    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
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Arraylist Code Error
  1. Fuad Hasan
    Replies:
    22
    Views:
    951
  2. UUXIII
    Replies:
    8
    Views:
    624
  3. OldGrantonian
    Replies:
    4
    Views:
    484
  4. infomamun
    Replies:
    3
    Views:
    504
  5. number5alive
    Replies:
    1
    Views:
    1,051
  6. JHolt
    Replies:
    0
    Views:
    857
  7. curvenger
    Replies:
    3
    Views:
    1,199
  8. MoodyBlues
    Replies:
    19
    Views:
    580
  9. RATKEN
    Replies:
    0
    Views:
    1,626
  10. Fred Pino
    Replies:
    2
    Views:
    2,929

Share This Page

Loading...