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

  • SEARCH
  • Home
  • 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 6146089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:53:40+00:00 2026-05-23T18:53:40+00:00

So I had my listview working perfectly then I decided to add a context

  • 0

So I had my listview working perfectly then I decided to add a context menu. As soon as I did that whenever I normal clicked an item in my listview, the entire list gets inverted on the first click. Subsequent clicks do nothing to the order, but when the first item is de-selected again the list returns to normal. When I take out the context menu logic that I added, the list view problem does not go away.

I’ve attached a debugger and the elements in my list adapter are never reordered, and the ListView itself is never set to reverse with .setStackFromBottom()

Here is my onClick listener registered to handle the click events of the list view items:

public void onClick(View v) {  
    ViewHolder holder = (ViewHolder) v.getTag();  
    CheckBox b = holder.box;  
    Boolean check = b.isChecked();  
    b.setChecked(!check);  
    if (!check) {  
       mChecked.add(holder.fu);  
       if (mChecked.size() == 1) {  
         buttonLayout.setVisibility(View.VISIBLE);  
       }  
    } else {  
       mChecked.remove(holder.fu);  
       if (mChecked.size() == 0) {  
         buttonLayout.setVisibility(View.GONE);  
       }  
   }  
}

The viewholder class just holds references to objects I use in the listview for optimizations. I cannot figure out why this is causing my list to invert when displayed, I’ve tried moving the listener to a different view in the layout, I’ve tried re-writing the listener, nothing seems to work! Any advice would be appreciated.

Edit: here is the code for the view holder

/** Class to provide a holder for ListViews. Used for optimization */  
private class ViewHolder {  
    TextView date;  
    TextView gallons;  
    TextView cost;  
    TextView cpg;  
    TextView mpg;  
    CheckBox box;  
    FillUp fu;  
}  

as well as the adapter:

 public class FillUpAdapter extends BaseAdapter {

        ArrayList<FillUp> mElements;
        ArrayList<FillUp> mChecked;
        Context mContext;

        public FillUpAdapter(Context c, ArrayList<FillUp> data) {
            mContext = c;
            mElements = data;
            mChecked = new ArrayList<FillUp>();
        }

        public void clearChecked() {
            mChecked.clear();
        }

        public ArrayList<FillUp> getChecked() {
            return mChecked;
        }

        public boolean remove(FillUp f) {
            mChecked.remove(f);
            return mElements.remove(f);
        }

        @Override
        public int getCount() {
            return mElements.size();
        }

        @Override
        public Object getItem(int arg0) {
            return mElements.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {

            return mElements.get(arg0).getId();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout layout;
            ViewHolder holder;
            if (convertView != null) {
                layout = (LinearLayout) convertView;
                holder = (ViewHolder) layout.getTag();
            } else {
                layout = (LinearLayout) LayoutInflater.from(mContext).inflate(
                        R.layout.fillup_list_item, parent, false);
                holder = new ViewHolder();
                holder.cost = (TextView) layout
                        .findViewById(R.id.fillUpListTotalValue);
                holder.cpg = (TextView) layout
                        .findViewById(R.id.fillUpListCostPerGal);
                holder.gallons = (TextView) layout
                        .findViewById(R.id.fillUpListGalValue);
                holder.mpg = (TextView) layout
                        .findViewById(R.id.fillUpMPGText);
                holder.date = (TextView) layout
                        .findViewById(R.id.fillUpListDate);
                holder.box = (CheckBox) layout
                        .findViewById(R.id.fillUpListCheckBox);
                holder.fu = (FillUp) getItem(position);
                layout.setTag(holder);

            }

            holder.date.setText(holder.fu.getDate());
            holder.gallons.setText(holder.fu.getGallonsText());
            holder.cpg.setText(holder.fu.getCostText());
            holder.cost.setText(holder.fu.getTotalText());
            holder.mpg.setText(String.format("%03.1f MPG",holder.fu.getMPG()));
            if (convertView != null) {
                holder.box.setChecked(mChecked.contains(holder.fu));
            }

            layout.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ViewHolder holder = (ViewHolder) v.getTag();
                    CheckBox b = holder.box;
                    Boolean check = b.isChecked();
                    b.setChecked(!check);
                    if (!check) {
                        mChecked.add(holder.fu);
                        if (mChecked.size() == 1) {
                            buttonLayout.setVisibility(View.VISIBLE);
                        }
                    } else {
                        mChecked.remove(holder.fu);
                        if (mChecked.size() == 0) {
                            buttonLayout.setVisibility(View.GONE);
                        }
                    }
                }
            });
            return layout;
        }
}

UPDATE:
Ok, so I’ve narrowed it down to the visibility change on the buttonLayout view, which is a linear layout of buttons on the bottom of the Activity’s layout, underneath the ListView. Whenever I change that view’s visibility to View.VISIBLE (which happens when the first item is checked) the list’s order is reversed. The order is restored when the view’s visibility is set to View.GONE

I have no idea what would cause that though 🙁

  • 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-23T18:53:40+00:00Added an answer on May 23, 2026 at 6:53 pm

    After narrowing the scope a bit more, I discovered the problem was not the changing of the visibility of my button bar, but actually the passing around of FillUp objects in holder.fu of my ViewHolder class. By changing that to instead reference the adapter’s getItem(position) method, everything seemed to work out. Quite an odd bug, since the adapter itself was not having the order of the elements changed, but passing around a reference to the object made it very unhappy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Had a page that was working fine. Only change I made was to add
I had to change a ListView webpart and noticed the syntax that renders the
I've created a custom listview which is working great apart from the fact that
Actually i want to change the text color of the listview that i had
I have a ListView that displays a shopping cart. It was working fine until
I had a custom listview with two textviews and an image view. On clicking
I had tried to to put checkbox in listview through layout inflator and I
Had a good search here but can't see anything that gets my mind in
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
Had this working; at one stage. The problem is the following text is now

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.