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 6913763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:15:32+00:00 2026-05-27T09:15:32+00:00

I have a button that should sort a ListView using a custom adapter. The

  • 0

I have a button that should sort a ListView using a custom adapter. The button listener is as follows:

collectionSort.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        sortOrder = -sortOrder;
        Log.d("DBGINF", "Sort Order: " + sortOrder);
        m.sortByTitles(sortOrder);
        m.notifyDataSetChanged();
});

The sort order changes as it should, and the data in the array is sorted, the actual view is never updated by the notifyDataSetChanged though. Here’s the sortByTitles() method:

public void sortByTitles(int dir) {
    if (dir > 0) {
        sort(new StringComparator());
    } else if (dir < 0) {
        sort(new ReverseStringComparator());
    }
    Log.d("Data Set: ", this.getItem(0) + "");
}

The notifyDataSetChanged() doesn’t ever update the view inside an OnClickListener, but always works properly outside one of the OnClickListeners. What am I doing wrong?

EDIT

Here is the constructor in the Adapter class:

public MainMenuArrayAdapter(Context context, CollectionObject[] objects) {
    super(context, R.layout.mainmenurow, objects);
    myContext = context;
}

And here’s how it gets initialized in the onCreate() method of the Activity:

CollectionObject[] sample = new CollectionObject[] { c1, c2, c3, c4, c5, c6 };
final MainMenuArrayAdapter m = new MainMenuArrayAdapter(this, sample);

Where c1 – c6 are just some temporary CollectionObjects for testing.

  • 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-27T09:15:33+00:00Added an answer on May 27, 2026 at 9:15 am

    What kind of adapter are you extending? I think you are actually sorting a copy of the items in your adapter. Are you setting myCollections in the adapter again? Btw if you extend ArrayAdapter keep in mind that the adapter itself has a sort method which accept your own comparator.

    EDIT:
    I wrote a super stupid example with a custom adapter and sorting, you can press menu for sorting items. Hope it will help!

    public class SortActivity extends ListActivity {
    
    private StupidAdapter stupidAdapter;
    private List<String> list = new ArrayList<String>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        list.add("Android-a");
        list.add("Android-b");
        list.add("Android-c");
        list.add("BlackBerry-a");
        list.add("BlackBerry-b");
        list.add("BlackBerry-c");
        list.add("Windows-a");
        list.add("Windows-b");
        list.add("Windows-c");
    
        // keep in mind that from now on whatever changes you do on list is not reflected on the adapter.
        // the array adapter keeps its own internal data
        stupidAdapter = new StupidAdapter(this, list);
        setListAdapter(stupidAdapter);
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        new MenuInflater(this.getApplication()).inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        final int id = item.getItemId();
        if (id == R.id.menu_main_sort_az) {
            stupidAdapter.sort(new Comparator<String>() {
                public int compare(String object1, String object2) {
                    return object1.compareTo(object2);
                };
            });
        } else if (id == R.id.menu_main_sort_za) {
            stupidAdapter.sort(new Comparator<String>() {
                public int compare(String object1, String object2) {
                    return object2.compareTo(object1);
                };
            });
        }
        stupidAdapter.notifyDataSetChanged();
        return true;
    }
    
    class StupidAdapter extends ArrayAdapter<String> {
    
        public StupidAdapter(Context context, List<String> list) {
            super(context, R.layout.list_item, list);
        }
    
        @Override
        public View getView(final int position, View row, final ViewGroup parent) {
            ViewHolder viewHolder;
    
            final String item = getItem(position);
    
            if (row == null) {
                row = getLayoutInflater().inflate(R.layout.list_item, parent, false);
                viewHolder = new ViewHolder(row);
    
                row.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) row.getTag();
            }
    
            viewHolder.refreshData(item);
    
            return row;
        }       
    
        class ViewHolder {
            TextView title;
            public ViewHolder(View row) {
                this.title = (TextView) row.findViewById(R.id.title);
            }
            public void refreshData(final String text) {
                this.title.setText(text);
            }
        }   
    
    }
    
    }
    

    list_item.xml

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffffff">
    
    <TextView 
        android:id="@+id/title"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:textSize="15sp"
        android:textColor="#ff000000"/>
    
     </LinearLayout>         
    

    main_menu.xml (under res/menu)

    <menu xmlns:android="http://schemas.android.com/apk/res/android">   
        <item android:id="@+id/menu_main_sort_az" android:title="Sort A-Z"/>
        <item android:id="@+id/menu_main_sort_za" android:title="Sort Z-A"/>
    </menu>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button <a> that should toggle a drop-down Then it should also
We have a button that saves asynchronously using AjaxToolKit/C#/.NET. I'm getting this in my
I have a button that, when pressed, will add a new HTML row. Within
I have a button that says Sort and when a user normal/short presses the
I have a group of buttons that should act like toggle buttons, but also
I have a button that I would like to disable when the form submits
I have a Button that is looking at 2 comboboxes to make sure they
I have a button that when clicked will run a stored procedure on a
I have a button that I'm creating in a UIViewController like so: TOLoginButton* button
I have a button that opens a dialog when clicked. The dialog displays a

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.