FET
FET

Reputation: 942

How to create dynamic Multidimensional ArrayList?

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

Upvotes: 0

Views: 688

Answers (2)

jBegera
jBegera

Reputation: 415

You can use ArrayList of ArrayLists.

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

multiDimensionalArrayList.add(new ArrayList<>());
multiDimensionalArrayList.get(0).add("...");

Upvotes: 3

kingfrito_5005
kingfrito_5005

Reputation: 617

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.

Upvotes: 0

Related Questions