My ultimate goal is to change the source of the information in this tutorial from an array to a cursor. Here is a link to the full code. The gist of it is that you click a row in the list, and the body pops open below the listed title, and you tap it again and the body is gone. I am not looking to remember if the note is open, nor am I looking to keep it open through view recycling, nor close all others when you open one or any other fancy permutation I can think of off the top of my head.
Everything mostly works, but when the onListItemClick event handler fires, changes the visibility, and notifyDataSetChanged()s, the list does strange things, including taking two clicks to change the visibility, and not remeasuring itself, leading to the list row only making room for itself every third click or so.
A previous attempt lead to a perfectly working list, excepting that on click, the chunk in every row that I wanted to hide and show would hide and show, and the list in the tutorial works perfectly, but of course uses static sizes in everything, so is little more than a template.
I am convinced that the problem is either getting the visibility information attached to the list row or changing it once it has been set.
Here is the onListItemClick:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
((NotesCursorAdapter) getListAdapter()).toggle(position, v);
}
with the toggle method inside the NotesCursorAdapter:
public void toggle(int position, View view) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.mExpanded[position] = !holder.mExpanded[position];
notifyDataSetChanged();
}
the ViewHolder outside of the NotesCursorAdapter:
static class ViewHolder {
public TextView title;
public TextView body;
public boolean mExpanded[];
}
and the NotesCursorAdapter itself:
class NotesCursorAdapter extends CursorAdapter {
private static final int VISIBLE = 0;
private static final int GONE = 8;
public NotesCursorAdapter(Context context, Cursor c) {
super(context, c);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, null, true);
ViewHolder holder = new ViewHolder();
holder.title = (TextView) rowView
.findViewById(R.id.title);
holder.body = (TextView) rowView
.findViewById(R.id.body);
holder.mExpanded = new boolean[cursor.getCount()];
rowView.setTag(holder);
return rowView;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.title.setText(cursor.getString(cursor
.getColumnIndexOrThrow(DbAdapter.KEY_TITLE))
+ holder.mExpanded[cursor.getPosition()]);
holder.body.setText(cursor
.getString(cursor
.getColumnIndexOrThrow(DbAdapter.KEY_BODY)));
holder.body
.setVisibility(holder.mExpanded[cursor.getPosition()] ? VISIBLE : GONE);
}
public void toggle(int position, View view) {
ViewHolder holder = (ViewHolder) view.getTag();
holder.mExpanded[position] = !holder.mExpanded[position];
notifyDataSetChanged();
}
}
I am at a loss on where to look next. Do I need to make my own getView() method? Would I get anything useful out of getItem()? Am I totally crazy for trying to use a listview like this?
I have done more investigating, and the code is working, but the on click events seem to be effecting the opposite views. What I mean by that, is that when you click the top list item, it effects the bottom list item. When you click the second from the top, it effects the second from the bottom. If there is an odd number of list items, the middle list item works perfectly. In some way, whatever way I am using to determine the id of the view that I am effecting is flipped. Does the listview number things from the bottom up?
K, I got it figured out. The settings were not displaying right because a ListView has two layers: the View layer and the data layer. The way that android recycles Views in a ListView means that which View is being used to display the data does not matter. So to use a CursorAdapter, the View information goes in the newView() method, and the data information from the Cursor goes in the bindView() method. Here is the fixed stuff:
Not much changed here, only passing what is needed:
The view holder only gets information needed to construct the view, not any information in the view, including the visible state:
The visible state is tracked in an array as a property of the adapter. The view information is in the newView() method and the data information is in the bindView() method. When the notifyDataSetChanged() fires, the views are all recycled, and the data is re-layered on. Because the view info and the data info are separate, it does not matter which view gets matched up with which data.
And for completeness, here is the mapping from the ListAdapter mapping to be called in onCreate(), onActivityResult(), or whenever the list needs reloaded: