gurehbgui
gurehbgui

Reputation: 14684

Why is my ListView not showing the new data?

my problem is, that I have a ListView and a ArrayAdapter. Now I create the adapter like this:

items = OtherClass.DataSet1;
adapter = new MyArrayAdapter(getActivity(), R.layout.mein_listitem, items);

then I want to change the items e.g:

items = OtherClass.DataSet2;
adapter.notifyDataSetChanged();

But the result is, that I still see the DataSet1 items in my ListView and I dont know why.

I have the following whole code:

public class PlaceholderFragment extends Fragment implements ActionBar.TabListener {

    ArrayList<Item> items;
    MyArrayAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        items = OtherClass.DataSet1;
        adapter = new MyArrayAdapter(getActivity(), R.layout.mein_listitem, items);

        {...}
    }

    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {

        if (arg0.getText().equals(getText(R.string.foo))) {
            items = OtherClass.DataSet1;
            adapter.notifyDataSetChanged();
        } else {
            items = OtherClass.DataSet2;
            adapter.notifyDataSetChanged();
        }
    }
}

public class Item {
    public Item(String foo1) {
        super();
        Foo1 = foo1;

    }

    public String Foo1;

}

Upvotes: 0

Views: 81

Answers (1)

nKn
nKn

Reputation: 13761

You have to add your items on your ArrayAdapter object via the .add() or .addAll() methods. This will actually append the objects you've provided as parameters to the dataset already showing up.

adapter.add(...);
adapter.addAll(...);

And (as you done), don't forget always calling .notifyDataSetChanged() on your adapter after adding items to it.

Upvotes: 2

Related Questions