Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you change the visibility of widgets inside individual listview rows from onlistitemclick in Android?
    primarykey
    data
    text
    <p>My ultimate goal is to change the source of the information in <a href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List6.html" rel="nofollow">this tutorial</a> from an array to a cursor. Here is a link to the <a href="http://pastebin.com/VYhwrnj2" rel="nofollow">full code</a>. 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.</p> <p>Everything mostly works, but when the onListItemClick event handler fires, changes the visibility, and <code>notifyDataSetChanged()</code>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. </p> <p>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.</p> <p>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.</p> <p>Here is the <code>onListItemClick</code>:</p> <pre><code>@Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); ((NotesCursorAdapter) getListAdapter()).toggle(position, v); } </code></pre> <p>with the toggle method inside the <code>NotesCursorAdapter</code>: </p> <pre><code> public void toggle(int position, View view) { ViewHolder holder = (ViewHolder) view.getTag(); holder.mExpanded[position] = !holder.mExpanded[position]; notifyDataSetChanged(); } </code></pre> <p>the <code>ViewHolder</code> outside of the <code>NotesCursorAdapter</code>:</p> <pre><code>static class ViewHolder { public TextView title; public TextView body; public boolean mExpanded[]; } </code></pre> <p>and the <code>NotesCursorAdapter</code> itself:</p> <pre><code>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(); } } </code></pre> <p>I am at a loss on where to look next. Do I need to make my own <code>getView()</code> method? Would I get anything useful out of <code>getItem()</code>? Am I totally crazy for trying to use a <code>listview</code> like this?</p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload