0

I have been teaching myself Java/XML with Android Studio and would appreciate some help. I am attempting to create a shopping list app and I am having a problem with naming string arrays inside of an array. I set up an ArrayList, but I would like to populate the ArrayList with string arrays. When I attempt to add a string input (from user), I get an error message:

add (java.lang.String[]) in List cannot be applied
to (java.lang.String)

Here is my code:

public class ShoppingLists extends AppCompatActivity {

    /* Create ArrayList (from List) and Adapter for the ListView */
    List<String[]> shoppingLists = null;
    ArrayAdapter<String> adapter = null;
    ListView lv = null;

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

        /* Create main array */
        shoppingLists = new ArrayList<>();

        /* Set view style with adapter */
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, shoppingLists);
        lv = (ListView) findViewById(R.id.shoppingListView);
        lv.setAdapter(adapter);
    }

    /* Create a menu. */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.shopping_menu, menu);
        return true;
    }

    /* Create actions for menu button presses */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        /* Determine which menu button is pressed */
        switch (item.getItemId()) {
            case R.id.action_add:
                /* Create an alert dialog for user input */
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(getString(R.string.add_list));
                final EditText input= new EditText(this);
                builder.setView(input);

                /* If 'OK' is pressed, enter user input as new object into shoppingLists array */
                builder.setPositiveButton(getString(R.string.ok_button), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        /* Convert user input to string and add it to list */
                        shoppingLists.add(input.getText().toString());
                        /* Update adapter with new object to make visible */
                        lv.setAdapter(adapter);
                    }
                });
                builder.setNegativeButton(getString(R.string.cancel_button), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        /* Exit dialog box without adding new object */
                        dialog.cancel();
                    }
                });
                builder.show();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

The error is underlined here:

shoppingLists.add(input.getText().toString());

I have a feeling I am missing something simple, but I can't figure it out.

Thanks in advance for your assistance!

1
  • 5
    You have a list of String[]. You're trying to add a String. Most likely the [] is a istake and you wanted to make shoppingLists a List<String> Commented Jan 31, 2017 at 5:30

2 Answers 2

1

There is two solutions

1- change List<String[]> shoppingLists = null; to List<String> shoppingLists = null;

2- First add your input text to a string array and then add that string array to the list as below: String[] strArray = {input.getText().toString()}; shoppingLists.add(strArray);

Sign up to request clarification or add additional context in comments.

Comments

1

List<String[]> shoppingLists = null; it should be

List<String> shoppingLists = null;

Comments

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.