0

i have a problem in storing the Item in the Array List here is 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<Item> itemlist = new ArrayList<Item>();
        itemlist.add(sparepart);

        PersonListAdapter adapter = new PersonListAdapter(this, R.layout.adapter_view_layout,itemlist);
        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);
    }



}

What i try to do is the the storing of item in the itemlist will be inform of automation through the array numbering. Therefore i will add the looping in the itemlist to store the item but the problem is when i want to make it into ArrayList[] itemlist = new ArraList[]; There is an error.

So what im tryna do is

public void onCreate(){
    int loop=0;
    loop++;
    String asd = intent.getStringExtra(MainActivity2.namabarang);
    String asdf= intent.getStringExtra(MainActivity2.jumlahstock);
    Item sparepart = new Item(asd,asdf);
    ArrayList<item>[] itemlist= new ArrayList[]();
    itemlist[loop] = sparepart;
}

So that everytime the MainActivity3 running it does save string in array 0, and then running in another Activity, when back to the MainActivity3 the string will save in Array 1. Please the help

  • mybe you tried to use a weak translator, we cann't understand you very well and you should add more details about what do you want to do and where is the error? – a7d.24_ Aug 31 '21 at 15:40
  • The error is in ArrayList listview to make it into array i try to put ArrayList[] listview=new ArrayList[]. The purpose is to save the list item in the Arraylist into specific number of array. –  Aug 31 '21 at 16:20
  • Or the error is caused by the adapter in the other pages? –  Aug 31 '21 at 16:25
  • What im tryna do is when the time directed into MainAcitivity3 it will get data of the item and store it into the array[0]. And it will go back to other MainAcitivity when the button click. When it directed to MainActivity3 the second time it will store the item in the array[1] and repeat again. –  Aug 31 '21 at 16:32

1 Answers1

0

You have a wrong syntax. To create array of ArrayList, the syntax is:

ArrayList<Item>[] itemlist = new ArrayList[10];

You need to specify the size at the initialization because it is an array, the size is fixed. Also it is better to use ArrayList of ArrayList instead of array of ArrayList.

Related topic: How can I create an Array of ArrayLists?

  • when using arraylist of arraylist i think i cannot point a specific number of array like array[0] and array[1], because im planning that it will save an item in array when each button click, through array i can use int integer++; to auto generate and save item when the apps is running –  Sep 02 '21 at 11:33
  • Here is another error when changing into the code above. –  Sep 02 '21 at 11:36
  • error: incompatible types: ArrayList[] cannot be converted to ArrayList PersonListAdapter adapter = new PersonListAdapter(this, R.layout.adapter_view_layout,itemlist); –  Sep 02 '21 at 11:37
  • When changing into PersonListAdapter adapter = new PersonListAdapter(this, R.layout.adapter_view_layout,itemlist[0]); mListView.setAdapter(adapter); the apps is forced close –  Sep 02 '21 at 11:39
  • @Daniel You can use arrayList.add(element), arrayList.get(index) and arrayList.set(index, element) right? – Bryan Amirul Husna Sep 03 '21 at 15:38
  • i have using Arraylist[] itemlist = new ArrayList[]; into adapter that must using Arraliyst. –  Sep 05 '21 at 02:05
  • But i have solve it by changing the element that the one using array –  Sep 05 '21 at 02:06