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. Sunny Rio
    Replies:
    1
    Views:
    405
  2. Kaweesi Samson
    Replies:
    1
    Views:
    1,067
  3. Dev101
    Replies:
    0
    Views:
    1,017
  4. Fatbomb
    Replies:
    2
    Views:
    677
  5. Denis Bogatov
    Replies:
    2
    Views:
    901
  6. CryogenWare
    Replies:
    10
    Views:
    705
  7. Becca Lou
    Replies:
    0
    Views:
    731
  8. App Update
    Replies:
    0
    Views:
    471
  9. insortani
    Replies:
    1
    Views:
    394
  10. RATKEN
    Replies:
    0
    Views:
    2,646

Share This Page

Loading...