prakash .k
prakash .k

Reputation: 635

Textview multiple clicks in android

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();             
}

Upvotes: 0

Views: 272

Answers (5)

Shubhayu
Shubhayu

Reputation: 13552

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

Upvotes: 0

Vinay W
Vinay W

Reputation: 10190

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";
        }
    }
});

Upvotes: 1

Lalit Poptani
Lalit Poptani

Reputation: 67286

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");
        } 
    }
});

Upvotes: 1

Daud Arfin
Daud Arfin

Reputation: 2499

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
         }                
    }
});

Upvotes: 1

sunil
sunil

Reputation: 6614

just place the

boolean flag = false;

above the function call

public void onClick(View v) {

this will do your task

Upvotes: 1

Related Questions