Anna Lam
Anna Lam

Reputation: 807

Android: notifyDataSetChanged is not working

The shopping list application I am attempting as an exercise has the following activities:

  1. ShoppingListMain - A list of the shopping lists
  2. ShoppingListActivity - One that displays all of the shopping list items for a particular shopping list
  3. ListItemEditActivity - One that allows one to edit the details of a shopping list item

When I add a new shopping list (in activity #1), the ListView refreshes perfectly. However, when I add a new shopping list item (in activity #2), I have to hit the back button and return to whichever shopping list I added the new item to for said item to appear in the ListView.

I have already tried the suggestions in the following post/s, but still no luck with ShoppingListActivity's ListView.

Any help with this problem would be greatly appreciated.

The code that I used for adding new shopping lists...

ShoppingListApplication.java

public void insertShoppingList(ShoppingList shoppingList) {
    assert(shoppingList != null);

    long id = mDbAdapter.insertShoppingList(shoppingList.getName());
    shoppingList.setId(id);
    mShoppingLists.add(shoppingList);
}

ShoppingListMain.java

    mNewListAddButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = mNewListName.getText().toString();
            ShoppingList newList = new ShoppingList();
            newList.setName(name);

            mApp.insertShoppingList(newList);

            mNewListName.setText("");
            mListAdapter.notifyDataSetChanged();
        }
    });

For adding new items to individual shopping lists...

ShoppingListActivity.java

    long id = mDbAdapter.insertListItem(mParentListId,
            item.getDescription(), item.getQuantity(),
            item.getUnitCost(), item.isPurchased(),
            item.getNotes());
    Log.i("ShoppingList", "New ID " + id);
    item.setId(id);

    mListItems.add(item);
    mListAdapter.notifyDataSetChanged();
    setListTotal();

Upvotes: 0

Views: 1627

Answers (1)

Agarwal
Agarwal

Reputation: 11

public void insertShoppingList(ShoppingList shoppingList) {
    assert(shoppingList != null);
    long id = mDbAdapter.insertShoppingList(shoppingList.getName());
    shoppingList.setId(id);
    mShoppingLists.add(shoppingList);
    mListAdapter.notifyDataSetChanged();
}

You must call adapter.notifyDataSetChanged() manually.

Upvotes: 1

Related Questions