2

I have a listView in my application and at some point during the execution, I want to highlight a specific row. I found some examples but I keep getting errors. Here is my code:

public void animate(int pos){ //pos:position of the row in the list
    lv1=(ListView)findViewById(R.id.listView1);
    Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.scale);
    ((View)lv1.getItemAtPosition(pos)).startAnimation(animation);
}

and scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
</set>

If anyone can help with this I would really appreciate it.

EDIT: I want to give an additional information I just found out. I get a NullPointerException when I run the code. Following line causes the exception

((View)lv1.getItemAtPosition(pos))

I cannot get the items in listView. I changed my program as follows:

public void animate(int pos){ //pos:position of the row in the list
    lv1=(ListView)findViewById(R.id.listView1);
    View v=lv1.getChildAt(pos);
    Toast.makeText(getBaseContext(),Boolean.toString((v==null)),Toast.LENGTH_SHORT).show();
}

It always displays "true". I didn't understand where the problem is stemming from.

yildirimyigit
  • 3,013
  • 4
  • 25
  • 35
  • 1
    What errors are you getting, what is not working ? – Noureddine AMRI Dec 16 '11 at 12:24
  • On my phone, I got this "... has stopped unexpectedly" message but unfortunately I don't know how I can learn what is causing it, I am a newbie. I get rid of the error when I comment out the following line: ((View)lv1.getItemAtPosition(pos)).startAnimation(animation); – yildirimyigit Dec 16 '11 at 12:27
  • Please check your [log](http://stackoverflow.com/q/2882253/741249) and post the stacktrace of the error. – THelper Dec 16 '11 at 12:59
  • Thanks THelper, I looked into my log and found out that I got a NullPointerException here: (View)lv1.getItemAtPosition(pos). However, I haven't been able to get the row in the listview properly. – yildirimyigit Dec 16 '11 at 14:12

1 Answers1

1

I'm not sure if this helps you at all, but I found your code useful to me to solve my problem. I got the same type of effect working with the following code:

//Animation swipe to the right
View view = lv.getChildAt(pos);
Animation animation = AnimationUtils.loadAnimation(TaskMain.this, R.anim.slideright);
view.startAnimation(animation);

Then I just put that in my area when I want to activate it. For myself in particular I activated on gesture.

rwarner
  • 577
  • 2
  • 4
  • 14