1

EDIT: Need to know how to create an ArrayList filled by a Textview

I am trying to create an ArrayAdapter for a Multidimensional (2D) ArrayList:

        final ArrayAdapter<ArrayAdapter<ArrayList<String>>> adapterNames = new ArrayAdapter<ArrayAdapter<ArrayList<String>>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayNamesOne);

This is the log error I get:

Error:(28, 76) error: no suitable constructor found for ArrayAdapter(MainActivity,int,int,ArrayList>) constructor ArrayAdapter.ArrayAdapter(Context,int,int,ArrayAdapter>[]) is not applicable (argument mismatch; ArrayList> cannot be converted to ArrayAdapter>[]) constructor ArrayAdapter.ArrayAdapter(Context,int,int,List>>) is not applicable (argument mismatch; ArrayList> cannot be converted to List>>)

If you prefer to take a better look at the whole code interested, here you go:

public class MainActivity extends ActionBarActivity {
//1ST DECLARATION
ArrayList<ArrayList<String>> arrayNamesOne = new ArrayList<ArrayList<String>>();
ListView listNamesOne;
TextView namesTextOne;

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


    //1ST SETUP
    listNamesOne = (ListView) findViewById(R.id.listNamesId);
    namesTextOne = (TextView) findViewById(R.id.namesTexter);

    final ArrayAdapter<ArrayAdapter<ArrayList<String>>> adapterNames = new ArrayAdapter<ArrayAdapter<ArrayList<String>>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayNamesOne);
    listNamesOne.setAdapter(adapterNames);
    //END 1ST SETUP

    //1ST BUTTON '+'
    Button buttonPlus = (Button)findViewById(R.id.buttonPlus);
    buttonPlus.setOnClickListener(
            new Button.OnClickListener() {
                //CLICK EVENT
                @Override
                public void onClick(View v) {
                    //IF EMPTY
                    if (namesTextOne.getText().toString().isEmpty()){
                        Context context = getApplicationContext();
                        CharSequence text = "Add an item first";
                        int duration = Toast.LENGTH_SHORT;
                        Toast.makeText(context, text, duration).show();
                    //IF SPACES ONLY
                    }else if(namesTextOne.getText().toString().trim().isEmpty()){
                        Context context = getApplicationContext();
                        CharSequence text = "You can't use spaces only";
                        int duration = Toast.LENGTH_SHORT;
                        namesTextOne.setText("");
                        Toast.makeText(context, text, duration).show();
                    //ALRIGHT, ADD IT!
                    } else if(!namesTextOne.getText().toString().isEmpty()) {
                        arrayNamesOne.add(0, (ArrayList<String>) namesTextOne.getText());
                        adapterNames.notifyDataSetChanged();
                        namesTextOne.setText("");
                    }

                }
            }
    );
}
eightShirt
  • 1,457
  • 2
  • 15
  • 29
FET
  • 942
  • 4
  • 13
  • 35
  • Well if you're trying to make an ArrayAdapter for an ArrayList of ArrayLists then you should change your call to reflect that. Right now you're making an ArrayAdapter for ArrayAdapters of ArrayLists. Is that what you want? – David M Jul 08 '15 at 19:57
  • No I want the first thing you said, you're right ;) – FET Jul 08 '15 at 20:06
  • Instead of a 2 dimensional array, you should probably consider using a `GridView` or a `RecyclerView` with a `GridLayoutManager`. – tachyonflux Jul 08 '15 at 20:07
  • @karaokyo Yes I'll go deep inside the RecyclerView as sson as I complete this version of the app, for now I'd like to finish it this way – FET Jul 08 '15 at 20:10

1 Answers1

1

If you were creating an adapter for a list of strings it would like this:

ArrayList<String> array = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, array);

So an adapter for a list of lists of strings would look like:

ArrayList<ArrayList<String>> array = new ArrayList<ArrayList<String>>();
ArrayAdapter<ArrayList<String>> adapter = new ArrayAdapter<ArrayList<String>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, array);
tachyonflux
  • 20,103
  • 7
  • 48
  • 67
  • Awesome, but now this line of code isn't working: `(ArrayList) namesTextOne.getText());` @karaokyo – FET Jul 08 '15 at 20:13
  • This is the error: Unchecked cast: 'java.lang.CharSequence' to 'java.util.ArrayList' less... (Ctrl+F1) Signals places where an unchecked warning is issued by the compiler, for example: void f(HashMap map) { map.put("key", "value"); } Hint: Pass -Xlint:unchecked to javac to get more details. – FET Jul 08 '15 at 20:14
  • You're casting a `CharSequence` to an `ArrayList`. What makes you think that would work? – tachyonflux Jul 08 '15 at 20:23
  • I never thought that would have worked :( I'm still learning, I'm in the Android world since yesterday.. How should I cast it then? @karaokyo – FET Jul 08 '15 at 20:27
  • http://stackoverflow.com/questions/7347856/how-to-convert-a-string-into-an-arraylist – tachyonflux Jul 08 '15 at 20:47
  • Will it work even for this 2D ArrayList right? @karaokyo – FET Jul 08 '15 at 20:50
  • It works and it doesn't.. It displays them, but in brackets [] – FET Jul 08 '15 at 20:56
  • I think it would probably be a good idea for you to just use the regular list of string as I showed in the first example. You can easily just add what ever strings you need to your array: `array.add(namesTextOne.getText())` – tachyonflux Jul 08 '15 at 21:18
  • Sorry for asking but, that will be possible to plume the on my already created 2D ArrayList? – FET Jul 08 '15 at 21:48
  • what do you mean by plume? – tachyonflux Jul 08 '15 at 21:58
  • Perhaps you can give more detail on what you are trying to accomplish. Why do you need to use a 2D array? Do you have the knowledge to even use a simple string adapter? Have you ever created a custom `ArrayAdapter`? It seems like you are trying to run before you know how to walk. – tachyonflux Jul 08 '15 at 21:59
  • That was a typo error sorry, it was implement. By the way this is what I'm creating: A button (I.e. B1) that adds Items to a list, each item of that list if clicked brings you to another activity which is the SANE of the first one, but it adds items to the item of the first list, that's why I thought about a 2D ArrayList, each item of the FIRST list is an Array List itself. @karaokyo – FET Jul 08 '15 at 22:31