views:

63

answers:

2

EDIT 2:

I did solve my problem, but i don't know how:S I was moving my code snippets a bit around, a suddenly it worked. Must have done something in the wrong order, but its weird, checked it many times. Thanks for you help, and sorry I can't post an answer ;)

Hi.

I have a list view which I'm trying to refresh to update it self when i add an element to the underlying array list.

Here is the code snippet:

private void addEvent() {
    arrlEvents.add( event );
    adptEvents.notifyDataSetChanged();
    updateSaveFile();
    filterList();
}

The arrlEvents is the underlying arraylist with the events, and im adding one event, trying to update the list view with notifyDataSetChanged(), but it doesnt work. Can anyone help?

Thanks for your time:)

EDIT: Here is the source code for the adapter:

private ArrayAdapter<Event> adptEvents;
adptEvents = new ArrayAdapter<Event>( EventCalendar.this, R.layout.list_items, arrlEvents );
A: 

I have seen that sometimes it just randomly doesnt notify the adapter.

Try using adptEvents as protected or public on a global scope.

I have found that when that doesnt work. You can just re set the adapter again, just substitute the notifyDataSetChanged() for:

adptEvents = new ArrayAdapter<Event>( EventCalendar.this, R.layout.list_items, arrlEvents );

Edit:

Heres a code snipper from an App I wrote that works.

Class definition:

public class ClassName extends ListActivity implements AdapterView.OnItemSelectedListener {

Global Variable:

CustomAdapter adapter;

in OnCreate():

adapter = new CustomAdapter(this,R.layout.layout_name,dataSet);
setListAdapter(adapter);

Whenever I need to notify

adapter.notifyDataSetChanged();
blindstuff
Doesn't work, thanks anyway:)
lands
blindstuff
This is what I ended up with, except the implements part. Choosing it as the right answer ;)
lands
A: 

There is no persistent link between arrlEvents and the adptEvents.... the latter simply initialises itself with the elements from the former. adptEvents has no way to know when arrlEvents changes.

To add new items you should call adptEvents.add(event) and not bother calling notifyDataSetChanged() explicitly, since ArrayAdapter.add() does that for you automatically.

Reuben Scratton
thanks for the tip
lands
It's more of an answer than a tip really... :)
Reuben Scratton