Reputation: 347
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!
Upvotes: 0
Views: 2006
Reputation: 1314
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);
Upvotes: 1
Reputation: 344
List<String[]> shoppingLists = null;
it should be
List<String> shoppingLists = null;
Upvotes: 1