Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just implemented something similar to this from List6 of API Demos as well. Mine is working perfectly (all be it I had trouble with having focus from an ImageButton). I used a SimpleCursorAdapter however I suppose you could use a CursorAdapter. I modified your code quite a bit. </p> <p>Not sure why you also added the View parameter so I took that out.</p> <pre><code>private ArrayList&lt;Boolean&gt; mExpanded; @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); ((NotesCursorAdapter) getListAdapter()).toggle(position); } </code></pre> <p>Your ViewHolder. Pretty simple, just that the constructor receives parameters from your getView.</p> <pre><code>private class ViewHolder { private TextView title; private TextView body; public ViewHolder(View convertView, boolean expanded) { title = (TextView) convertView.findViewById(R.id.title); body = (TextView) convertView.findViewById(R.id.body); //not sure if you need this... //body.setVisibility(expanded ? View.VISIBLE : View.GONE); } public void setTitle(int position) { cursor.moveToPosition(position); title.setText(cursor.getString(cursor .getColumnIndexOrThrow(PowersDbAdapter.KEY_TITLE))); } public void setBody(int position) { cursor.moveToPosition(position); body.setText(cursor.getString(cursor .getColumnIndexOrThrow(PowersDbAdapter.KEY_BODY))); } public void setExpanded(boolean expanded) { body.setVisibility(expanded ? View.VISIBLE : View.GONE); } } </code></pre> <p>Here is your custom ListAdapter. I am not sure why you wanted to use bindView and newView with a ViewHolder, Android takes care of that with bindView and newViews. But you need to use a ViewHolder in this case so getView is necessary.</p> <pre><code>private class NotesCursorAdapter extends SimpleCursorAdapter { private Cursor cursor; private LayoutInflater inflater; public NotesCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) { super(context, layout, cursor, from, to); this.cursor = cursor; inflater = LayoutInflater.from(context); mExpanded = new ArrayList&lt;Boolean&gt;(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.row, parent, false); populateExpanded(); holder = new ViewHolder(convertView, mExpanded.get(position)); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.setTitle(position); holder.setBody(position); holder.setExpanded(mExpanded.get(position)); return rowView; } private void populateExpanded() { // We want our hidden TextView to not be expanded initially for (int i = 0; i &lt; cursor.getCount(); i++) { mExpanded.add(false); } } public void toggle(int position) { if (mExpanded.get(position) == false) { mExpanded.set(position, true); } else { mExpanded.set(position, false); } notifyDataSetChanged(); } } </code></pre> <p>Here is how you can map it with a SimpleCursorAdapter</p> <pre><code>setListAdapter(new NotesCursorAdapter(MyClass.this, R.layout.row, cursor, new String[] { "my_title", "my_body" }, new int[] { R.id.MyTitleTextView, R.id.MyBodyTextView })); </code></pre>
    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.
    1. This table or related slice is empty.
    1. COI am trying to get your code to work. Where is setExpanded() called and how can I set the size of the array on the holder when the holder is null? I had the view as a parameter because I was trying different things, and so I made the constructor to accept whatever I could get. I am using CursorAdapter instead of a SimpleCursorAdaptor because I am eventually going to be doing non-simple things, and I don't think that SimpleCursorAdapter is meant to do much more than map strings to TextViews and ImageViews. CursorAdapter requires code for the newView() and bindView() methods.
      singulars
    2. COOk I fixed some errors. I created a method to set initial values to the boolean array. Just like they predefined it in List6 (but that was a static cheap way and not dynamic). SimpleCursorAdapter can do everything you wanted. In my app, I have a textview (like your title) and a linear layout under it that is has its visibility as GONE. It contains two imagebuttons (with onclicklisteners) and a textview (displays a number). It is very non simple. Exactly what do you have in mind you are going to do later on?
      singulars
    3. COI got it figured out. The problem was that I was tracking the status of the expanded view in both the adapter and the view holder. Once I added a boolean array into the adapter class and then used that to determine the state of the views, the app started doing stranger stuff, and when I removed the visibility boolean from the view holder, it worked perfectly. Thank you so much for your help. Making me try a simple cursor adapter led me to really look at how the getView()/newView()/bindView() system works, and made me really take a hard look at where the objects were coming from.
      singulars
 

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