I'm sorry to write about what seems to be a common problem, but I've already been on this one task for five hours, and I simply give up.
I'm making a small android app, all it's supposed to do right now is keep a small SQLite database and be able to write/edit notes on it. Everything works fine with SQL, my problem is with the ListView. No matter what I do, it just refuses to update when I add a new note. The only way to view the new note is when I enter edit mode in another and come back out.
Here's the relevant part of the source:
public class MainActivity extends Activity {
ArrayAdapter<NoteItem> mAdapter;
public static NoteDataBase ndb;
ListView listView;
ArrayList a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ndb = new NoteDataBase(this);
a = ndb.getAllNotes();
//LISTVIEW DO NOT TOUCH
/***********/ mAdapter = new ArrayAdapter<NoteItem>(this,
/***********/ android.R.layout.simple_list_item_1, a);
/***********/ listView = (ListView) findViewById(R.id.listView1);
/***********/ listView.setAdapter(mAdapter);
//*****************
}
//New Note button
public void newNoteAction(View view){
ndb.addNote(new NoteItem("Title", "Contents"));
a = ndb.getAllNotes();
mAdapter = new ArrayAdapter<NoteItem>
(this, android.R.layout.simple_list_item_1, a);
mAdapter.notifyDataSetChanged();
System.out.println(ndb.getAllNotes().size());
}
The print at the end is just to see if it's actually creating the new note.
I hope I gave enough info, tell me if you need anything else. I tried almost everything that I read already, but after almost five hours on one task, there's a point where I'm gonna go crazy :)
Thanks,
Shef
public void newNoteAction(View view){
ndb.addNote(new NoteItem("Title", "Contents"));
// don't do this
// you are creating a new adapter and not binding it back to listview
// a = ndb.getAllNotes();
// mAdapter = new ArrayAdapter<NoteItem>(this, android.R.layout.simple_list_item_1, a);
mAdapter.add(new NoteItem("Title", "Contents"));
mAdapter.notifyDataSetChanged();
System.out.println(ndb.getAllNotes().size());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With