ListView won't refresh until a button is pressed

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

ListView won't refresh until a button is pressed

Postby calebpburns » Thu Sep 17, 2009 11:23 pm

My problem is that I want my listview (which contains 'notes') to refresh or update when I add a note. It will update, but only if I press the menu button, or a key, and not once I click the "Add Note". Can any one help me? This is my first app.

Code: Select all
public class Note {
   public long id;
   public String body;
}

Code: Select all
public class MyActivity extends Activity {
   ListView mListView;
   //...
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.list);
      //...
      // setup list adapter
      List<Note> grabNotes = mDb.grabNotes(null); //returns an ArrayList<Note>, a list of notes
      NoteAdapter adapter = new NoteAdapter(this, R.layout.note_item, grabNotes);
      adapter.notifyDataSetChanged(true);
      mListView.setAdapter(adapter);
      //...
   }
   //...
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
         case R.id.note_add:
            long entryId = mDb.insertNote("Some note");
            if (entryId != -1) {
               NoteAdapter adapter = (NoteAdapter)mListView.getAdapter();
               List<Note> notes = mDb.grabNotes(MySQLiteDatabase.NOTES_COLUMN_ID + "=" + Long.toString(entryId));
               adapter.add(notes.get(0));
            }
            break;

         default:
            break;
      }
      return super.onOptionsItemSelected(item);
   }
}
calebpburns
Freshman
Freshman
 
Posts: 2
Joined: Thu Sep 17, 2009 9:50 pm

Top

Postby calebpburns » Fri Sep 18, 2009 8:01 pm

Never mind, I solved my problem by extending a ListActivity instead of an Activity
calebpburns
Freshman
Freshman
 
Posts: 2
Joined: Thu Sep 17, 2009 9:50 pm

Postby konic » Mon Sep 28, 2009 4:46 am

calebpburns wrote:Never mind, I solved my problem by extending a ListActivity instead of an Activity

Still the question remains - Why ListView is not updated after the
Code: Select all
adapter.add(notes.get(0));

:(
In fact, if one just goolges it a bit it is obviously that lots of people have the same question.
Standard "tutorials" either show the View updated after some KeyPressed or extends ListActivity.
And there is always just one list.
It is supposed to be a simple issue - add list or several lists that can be changed by just using add() or remove() methods.
I know that UI must be updated by Handler, but somehow it does not work only for ListView (!?),
although it does work for, say, TextView.
Last edited by konic on Mon Sep 28, 2009 1:09 pm, edited 1 time in total.
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Postby wiederke » Mon Sep 28, 2009 12:26 pm

Hi,

to ensure that the ListView refreshes call notifyDataSetChanged() on its adapter and ensure, that you really modified the data the adapter is using.


Cheers,

Torsten
wiederke
Experienced Developer
Experienced Developer
 
Posts: 72
Joined: Mon Mar 02, 2009 6:46 pm
Location: Germany

Postby konic » Mon Sep 28, 2009 12:46 pm

wiederke wrote:Hi,
to ensure that the ListView refreshes call notifyDataSetChanged() on its adapter and ensure,
that you really modified the data the adapter is using.
Torsten

Nope. This is again standard answer which does not work.
By default setNotifyOnChange is set to true, which means:
"that change the list (add(T), insert(T, int), remove(T), clear()) automatically call notifyDataSetChanged()"
(http://developer.android.com/reference/ ... ge(boolean)).
And of course, I tried to explicitly call that notifyDataSetChanged() without sucess :?
...
P.S. OK, I'll explain a bit my case. I am trying to consume some Web Service and display response as a List.
As a regular approach I create ExecutorService to access WS and receive data from it.
To update UI I use Handler.
After I am done with WS I call updateResult() which looks like:
Code: Select all
   Handler guiThread = new Handler();
   ....
   ....
   public void updateResult() {
      guiThread.postDelayed(new Runnable() {
         public void run() {
               eqAdapt.notifyDataSetChanged();
         }
      }, 200);
   }

The problem I observe is that method getView() of 'eqAdapt' (MyAdapter) is not called at all(!?)
Adapter is defined as following:
Code: Select all
class MyAdapter extends ArrayAdapter<SomeStuff> {
...
}
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Postby konic » Mon Sep 28, 2009 11:16 pm

OK, mystery solved !
The problems were in layout files for the list itself and items.
Actually, it was one problem, - I missed these two lines in each view:
Code: Select all
android:layout_width="wrap_content"
android:layout_height="wrap_content"

After I added them, everything started to work as it should.
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests