I have a listview NoteList which contains a method doListRefresh() to select list contents from the sqlite note table, populates a cursor (which populates my array adapter), then calls adapter.notifyDataSetChanged(); to refresh the list. Upon (add, edit, deletes etc)
Inside my custom Array adapter i populate several elements on each row including a delete button. Inside the array adapter i have the delete button onClick handler etc. I need to call NoteList.doListRefresh() from within my Array adapter and i do this by using a setter inside my array adapter which is called from NoteList something like adapter.setNoteListObj(this);. This is working but i’m not sure its the best way?
I wanted to ask if this is “best practice” for doing something like this?
Thank you for any advice
Here is some of the sample code:
Within NoteList:
onCreate(Bundle savedInstanceState) {
thisObject = this;
}
// set custom ArrayAdapter to the data
adapter = new NoteArrayAdapter(activity, R.layout.channel_note_list_item, noteList);
adapter.setNoteList(thisObject);
…
within Adapter:
private NoteList callingNoteListObj;
...
// setter
protected void setNoteList(NoteList _callingNoteListObj) {
this.callingNoteListObj = _callingNoteListObj;
}
...
// within delete onclick handler (after delete)
// refresh local database
callingNoteListObj.doListRefresh();
I don’t see why not. All you are doing is passing a reference to the instance (a “pointer” for those who have done more than Java). Personally, I’d pass
thisinto the constructor for your adapter and store the reference locally. In fact, you probably already do if you pass inthisas a Context object in order to construct anArrayAdapter. Since its your custom adapter for your custom NoteList, you could very well just castcontext(orthis.getContext()) to NoteList and call it without needing to use a setter.