0

I'm creating an Android App, and in order to make it work I need to create a Multidimensional ArrayList (2D) dynamically:

I am leaving you the code, in order to make you understand how it's been ordered and so you'll be able to clear it up to add this feature (possibly I'd like not just to have the pieace of code itself, but rather to have an almost detailed explanation of what's in there, I'll appreciate that really!

Anyway I'll also leave you some pictures to look at to focus a little bit more on the project, thank you in advance as always guys!

Java code:

public class MainActivity extends ActionBarActivity {

ArrayList<String> arrayNames = new ArrayList<String>();
ListView listNames;
TextView namesText;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //Creating and Printing Lists
    listNames = (ListView) findViewById(R.id.listNamesId);
    namesText = (TextView) findViewById(R.id.namesTexter);
    final ArrayAdapter<String> adapterNames = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayNames);
    listNames.setAdapter(adapterNames);


    Button buttonPlus = (Button)findViewById(R.id.buttonPlus);
    buttonPlus.setOnClickListener(
            new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (namesText.getText().toString().isEmpty()){
                        Context context = getApplicationContext();
                        CharSequence text = "You cannot add an empty Item!";
                        int duration = Toast.LENGTH_SHORT;
                        Toast.makeText(context, text, duration).show();

                    }else if(namesText.getText().toString().trim().isEmpty()){
                        Context context = getApplicationContext();
                        CharSequence text = "You cannot add an Item with spaces only!";
                        int duration = Toast.LENGTH_SHORT;
                        namesText.setText("");
                        Toast.makeText(context, text, duration).show();

                    } else if(namesText.getText().toString() != null && !namesText.getText().toString().isEmpty()) {
                        arrayNames.add(0, namesText.getText().toString());
                        adapterNames.notifyDataSetChanged();
                        namesText.setText("");
                    }

                }
            }
    );
}

Imgur mages to explain the project:

http://imgur.com/a/aJYoe

FET
  • 942
  • 4
  • 13
  • 35

2 Answers2

3

You can use ArrayList of ArrayLists.

ArrayList<ArrayList<String>> multiDimensionalArrayList = new ArrayList<ArrayList<String>>();

multiDimensionalArrayList.add(new ArrayList<>());
multiDimensionalArrayList.get(0).add("...");
jBegera
  • 405
  • 2
  • 12
  • This way with 0 I select the first ArrayList (the biggest one) ? @jBegera – FET Jul 08 '15 at 15:17
  • Nah I take the first item in the biggest one, guess so – FET Jul 08 '15 at 16:58
  • The biggest one item of "i" ArrayList you get by multiDimensionalArrayList.get(i).get( multiDimensionalArrayList.get(i).size()-1) – jBegera Jul 09 '15 at 09:48
0

ArrayLists are always dynamic in Java. If you want to instantiate it at run time, instantiate inside of a nonstatic (dynamic) method. Maybe you should rephrase your question to clarify, as it seems that what you are asking doesn't make a lot of sense.

kingfrito_5005
  • 611
  • 5
  • 15
  • Then I'll try to explain it practically: I need an Activity to add ITEMS to an ArrayList, where every ITEM needs to be an ArrayList itself, so when you click an ITEM of the 1st list it appears a new ArrayList full of OtherITEMS – FET Jul 08 '15 at 14:47
  • Okay, so you need to crate an ArrayList of Arraylists of items. Is the problem that you are having that you do not know what type Item will be? If so you can just use an 'Object' as a generic ( I think Java has generics, I could be wrong.) If not then I am still confused as to what the problem is. – kingfrito_5005 Jul 08 '15 at 20:58
  • Hey there @kingfrito_5005 look, I kinda solved this issue, but I don't know how to solve this other one (which is related to this one) : http://stackoverflow.com/questions/31302199/arrylist-adapter-constructor-fails/31302601? – FET Jul 08 '15 at 21:02