开发者

Android How sort and reverse sort in single Button clcik

I want to sort and reverse sorting in single button click but i able to did only one, how can i implement this sorting and reverse sorting in consecutive button clicks.

 btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Comparator<Book> bb=Collections.reverseOrder();
                bk=Arrays.asList(books);
               Collections.sort(bk,bb);
                //Collections.shuffle(bk);
             // Collections.sort(bk);
                bookListView.invalidateViews();

     开发者_如何学JAVA   }
    });


All your code is doing is sorting the list in reverser order.

Collections.reverseOrder() returns a comparator to reverse.

And you pass bb comparator to Collections.sort method which reverse the collection.

From java doc, Sorts the specified list according to the order induced by the specified comparator.

You should sort it after you reverse it.


Use a Boolean field whose initial value is true. In click handler of button, compare the boolean field value, if it is true then do sort and set false to boolean field. If boolean value is false then do reverse and set true to boolean field.

boolean state=true;

btn.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    if(state) {
        //sort
        state=false;
     }
   else{
      //reverse
      state=true;
     }
    }
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜