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

2
  • ArrayList<ArrayList<POJO>()>(); Commented Jul 8, 2015 at 13:58
  • @VishwajitPalankar I know how to create one, but I want to create one dynamically, try to take allok at the code :) Commented Jul 8, 2015 at 14:01

2 Answers 2

3

You can use ArrayList of ArrayLists.

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

multiDimensionalArrayList.add(new ArrayList<>());
multiDimensionalArrayList.get(0).add("...");
Sign up to request clarification or add additional context in comments.

3 Comments

This way with 0 I select the first ArrayList (the biggest one) ? @jBegera
Nah I take the first item in the biggest one, guess so
The biggest one item of "i" ArrayList you get by multiDimensionalArrayList.get(i).get( multiDimensionalArrayList.get(i).size()-1)
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.

3 Comments

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
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.
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) : stackoverflow.com/questions/31302199/…?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.