Office Hours — Today, February 13

Tuesday, February 11

Feb 13
7:20 PM
Mark M.
has entered the room
7:25 PM
Mark M.
turned on guest access
7:50 PM
Uno T.
has entered the room
Mark M.
hello, Uno!
how can I help you today?
Uno T.
View paste
How to get item onLisItemClick event ?
I use subclasses from SimpleCursorAdaptor
I try this below and it works.
Just want to know whether that is the best practice or not.
does it need ViewHolder via setTag and get ?


@Override
	public void onListItemClick(ListView parent, View v, int position, long id) {
		
		TextView member = (TextView) v.findViewById(R.id.title);
}
Mark M.
onListItemClick() is available to you in ListActivity and ListFragment
if you extend either of those classes, you can override onListItemClick()
Uno T.
ini ListFragment
Mark M.
to find the item, call getItem() on your Adapter, passing in the position
for a SimpleCursorAdapter, the return value of getItem() will be your Cursor, positioned at the proper row
you can then call methods on the Cursor like getString() and getInt() to retrieve values for columns out of that row
ViewHolder has nothing to do with onListItemClick(), generally speaking
if you have another question, go right ahead
Uno T.
the _id from SimpleCursorAdapter is mapped to long id or int position in onListItemClick view ?
7:55 PM
Mark M.
the _id is passed to you as the long id parameter in onListItemClick()
Uno T.
ok great.
you mean like this:
View paste
@Override
	public void onListItemClick(ListView parent, View v, int position, long id) {
		
		Cursor cr = (Cursor) getItem(id);
}
Mark M.
no
getItem() takes the position, not the id
and getItem() is a method on your SimpleCursorAdapter, not on ListFragment
Cursor cr=(CursorAdapter)getListAdapter().getItem(position)
8:00 PM
Uno T.
ok... great
other question:
I implement this code :
View paste
@Override
	public void onDestroy() {
		super.onDestroy();
		Log.i("Arisan", "onDestroy");
	((CursorAdapter) getListAdapter()).getCursor().close();
		db.close();
	}
and run on simulator
8:05 PM
Uno T.
when it's called because i switch the fragment then go back to that fragment. It's got an error : attempt to re-open an already-closed object sqlitequery
Mark M.
how are you getting the Cursor in the first place?
Uno T.
in onActivityCreated using AsyncTask: new LoadCursorTask().execute();
Mark M.
"because i switch the fragment then go back to that fragment" -- what do you mean?
8:10 PM
Uno T.
i mean there are two fragments, I move one to another by using Navigation Drawer
Mark M.
how are you doing this? by a FragmentTransaction?
Uno T.
in MainActivity:
View paste
private void showMembers() {
		if (members == null) {
			members = new MembersListFragment();
		}

		if (!members.isVisible()) {
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.content, members).commit();
		}
	}
Mark M.
are you creating a new instance of your ListFragment when you return to it? or are you running another replace() transaction to put the prior fragment instance back on the screen?
8:15 PM
Uno T.
hmm.. i think it's just replaced. because there is checking if (members==null) before creating it.
Mark M.
it feels like that you are holding onto something in your fragment that is associated with the old Cursor and SQLiteDatabase
however, the stack trace should point you to it
either clean that up, so it is re-initialized when your fragment is re-created, or switch to using detach() and attach() instead of replace()
Uno T.
ok
will try to dig it more
8:30 PM
Mark M.
that's a wrap for today's chat
the transcript will be posted shortly to http://commonsware.com/office-hours/
the next chat is tomorrow, 10am US Eastern Time
have a pleasant day!
Uno T.
has left the room
Mark M.
turned off guest access

Tuesday, February 11

 

Office Hours

People in this transcript

  • Mark Murphy
  • Uno Tursadi