Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6909497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:41:49+00:00 2026-05-27T08:41:49+00:00

My ultimate goal is to change the source of the information in this tutorial

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:41:50+00:00Added an answer on May 27, 2026 at 8:41 am

    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:

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        ((NotesCursorAdapter) l.getAdapter()).toggle(position);
    }
    

    The view holder only gets information needed to construct the view, not any information in the view, including the visible state:

    static class ViewHolder {
        public TextView title;
        public TextView body;
        public Button button;
    }
    

    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.

        class NotesCursorAdapter extends CursorAdapter {
    
        private boolean[] expandedArray;
    
        public NotesCursorAdapter(Context context, Cursor c) {
            super(context, c);
            expandedArray = new boolean[c.getCount()];
        }
    
        @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, parent, false);
    
            ViewHolder holder = new ViewHolder();
            holder.title= (TextView) rowView
                    .findViewById(R.id.title);
            holder.body= (TextView) rowView
                    .findViewById(R.id.body);
            holder.button= (Button) rowView
                    .findViewById(R.id.button);
    
            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(NotesDbAdapter.KEY_TITLE)));
            holder.body.setText(cursor.getString(cursor.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
            holder.body
                    .setVisibility(expandedArray[cursor.getPosition()] ? View.VISIBLE
                            : View.GONE);
            holder.button
                    .setVisibility(expandedArray[cursor.getPosition()] ? View.VISIBLE
                            : View.GONE);
            holder.button.setTag(cursor.getPosition());
    
        }
    
        public void toggle(int position) {
            expandedArray[position] = !expandedArray[position];
            notifyDataSetChanged();
        }
    }
    

    And for completeness, here is the mapping from the ListAdapter mapping to be called in onCreate(), onActivityResult(), or whenever the list needs reloaded:

    private void fillData() {
        notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);
        NotesCursorAdapter notes = new NotesCursorAdapter(this, notesCursor);
        setListAdapter(notes);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my ultimate goal... to take this xml file.. <?xml version=1.0?> <Songs> <Song>
Here's my ultimate goal in all of this. I have a viewcontroller with a
My ultimate goal is to allow users to select a file from a dialog
My ultimate goal is to do this programmatically, but as a sanity check I'm
This is not a programming question per se, although the ultimate goal is to
My ultimate goal is to change the background of a div through clicking on
The ultimate goal is to use JSFL to export a 2D skeleton from Flash.
My ultimate goal is to write a sql script that selects data from a
My ultimate goal for this project is to produce the correct number of active
My ultimate goal is to successfully link to a number of DLLs (opengl32.dll glfw.dll

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.