0

I recognize that there are plenty of questions and answers about the notifyDataSetChanged() method already on stackoverflow, but I've reviewed most of them and can't figure out what could be wrong here. I'm trying to get my listview to dynamically add more lines as the user clicks on the "Add Ingredient Button". It will add the first ingredient after the first click, but any subsequent clicks do not result in any change to the list view. Any help is appreciated.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_recipe);
    ButterKnife.bind(this);
    mAddInstructionsButton.setOnClickListener(this);
    mAddIngredientButton.setOnClickListener(this);
    adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ingredientList);
    mListView.setAdapter(adapter);
}

public void onClick(View v) {
    if(v == mAddIngredientButton) {
        if(mIngredientName.getText().toString().trim().equalsIgnoreCase("") || mIngredientMeasurement.getSelectedItem().toString().trim().equalsIgnoreCase("") || mIngredientCount.getText().toString().trim().equalsIgnoreCase("")) {
            Toast answerStatus = Toast.makeText(NewRecipeActivity.this, "Fill out all fields", Toast.LENGTH_LONG);
            answerStatus.show();
        } else {
            String ingredient = createIngredientString();
            ingredientList.add(ingredient);
            adapter.notifyDataSetChanged();
            clearIngredientInputs();
            Log.i("NewRecipeActivity", "List includes: " + ingredientList);
        }
    }
  • Show the error Logcat . – KeLiuyue Oct 14 '17 at 02:02
  • There's no error. The Logcat currently just shows that my ingredientList ListArray is definitely getting added to: 21476-21476/com.epicodus.myrestaurants I/NewRecipeActivity: List includes: [3 tsp dsf] 10-14 01:46:00.804 21476-21476/com.epicodus.myrestaurants I/NewRecipeActivity: List includes: [3 tsp dsf, 4 tsp fdg] 10-14 02:08:25.729 21476-21476/com.epicodus.myrestaurants I/NewRecipeActivity: List includes: [3 tsp dsf, 4 tsp fdg, 4 tsp gdfg] 10-14 02:08:31.449 21476-21476/com.epicodus.myrestaurants I/NewRecipeActivity: List includes: [3 tsp dsf, 4 tsp fdg, 4 tsp gdfg, 6 tsp dsf] – Trevor Gill Oct 14 '17 at 02:10
  • You can try [https://stackoverflow.com/questions/3669325/notifydatasetchanged-example](https://stackoverflow.com/questions/3669325/notifydatasetchanged-example) – KeLiuyue Oct 14 '17 at 02:12
  • ArrayAdapter contains a copy of the original list... not the original list itself. You're adding the ingredient to the original list, but not the copy inside the adapter. Since you're using an `ArrayAdapter`, @CamiloCons answer should add the item to the internal copy. – Jon Oct 14 '17 at 02:48

2 Answers2

0

You need to add the new Ingredient to the Adapter's list, using the method

adapter.add(ingredient)

like this

String ingredient = createIngredientString();
adapter.add(ingredient);
adapter.notifyDataSetChanged();
camilocons
  • 31
  • 2
0

Oh boy, I feel silly about this one. The code was working all along just as it was posted in my question. The listview element on my activity was set with a height of 52 pixels, so that any rows that were added to the listview did not appear. Spent about 4 hours reading posts and changing up all kinds of stuff in the java file, and it was just an issue with the height of the display element. Cue sad trombone sound