0

In my application I have a TextView, when the user clicks for thr first time on it I have to arrange the data in descending order,When user click on the same text view once again I have to arrange the data in ascending order.Is there any way to accomplish this?

My Code:

public void onClick(View v) {
    // TODO Auto-generated method stub
    boolean flag = false;
    db.open();
    Cursor c;
    if(flag==false)
    {
        c = db.getIncomeTitleDescitem(intent.getStringExtra("grpsdb"));
        startManagingCursor(c);
        flag=true;
    }                   
    else
    {
        System.out.println("inside else");
        c = db.getIncomeTitleAscitem(intent.getStringExtra("grpsdb"));
        startManagingCursor(c);
        flag=false;
    }
    ListView lv=(ListView)findViewById(R.id.listView1);     
    String[] fromdes = new String[] {db.KEY_DATE,db.KEY_DESC,db.KEY_INCOME,db.KEY_TOTAL};
    int[] todes = new int[] {R.id.text1 ,R.id.text3,R.id.text5,R.id.text7};
    SimpleCursorAdapter notes = new SimpleCursorAdapter(IncomeDetails.this, R.layout.columnview, c, fromdes, todes);                          
    lv.setAdapter(notes);  
    db.close();             
}
Rob
  • 4,927
  • 12
  • 49
  • 54
prakash .k
  • 635
  • 2
  • 12
  • 24

5 Answers5

1

You can use setTag() and getTag() to maintain the state.

By default set the TextView as tv.setTag("descending"); then inside onClick() use getTag() and perform your work.

Sample,

tv.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View paramView) {
        String order = (String) paramView.getTag();
        if(order.equalsIgnoreCase("descending")){
            // perform descending ordering
            tv.setTag("ascending");
        }
        else if (order.equalsIgnoreCase("ascending")) {
            // perform ascending ordering
            tv.setTag("descending");
        } 
    }
});
Rob
  • 4,927
  • 12
  • 49
  • 54
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
1

do you have the logics of arranging them in ascending and descending order?

If yes,

suppose the functions arrangeA() and arrangeD() do the arranging job, in ascending and descending order respectively.

String lastSet="none";
TextView tv= (TextView) findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if(lastSet.equals("none")||lastSet.equals("asc")) {
            arrangeD();
            lastSet="desc";
        }
        else {
            arrngeA();
            lastSet="asc";
        }
    }
});
Rob
  • 4,927
  • 12
  • 49
  • 54
Vinay W
  • 9,912
  • 8
  • 41
  • 47
1

Yes you can easily track with a flag, based on click update the flag.. flag you can keep as boolean or int whatever you want. For your reference...

boolean flag = false;   // false - first click, true - second click
textView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (flag == false) {
            flag = true;
            // do descending
         } else {
             flag = false;
             // do ascending
         }                
    }
});
Rob
  • 4,927
  • 12
  • 49
  • 54
Daud Arfin
  • 2,499
  • 1
  • 18
  • 37
  • i checked your code, you are doing a big mistake by creating boolean flag inside onClick. You should create one time and need to be track inside onClick... what else you are looking for ? – Daud Arfin Jul 19 '12 at 10:24
1

just place the

boolean flag = false;

above the function call

public void onClick(View v) {

this will do your task

sunil
  • 6,444
  • 1
  • 32
  • 44
0

boolean sortAscend = false

In your TextView's onclicklistener() check for sortAscend and sort it and set sortAscend = !sortAscend.

This way it will keep sorting it in asc/desc alternately

Rob
  • 4,927
  • 12
  • 49
  • 54
Shubhayu
  • 13,402
  • 5
  • 33
  • 30