0

I would like to add this slide in from left animation to my listView when I scroll

android:fromXDelta="100%p"
android:toXDelta="0"
android:duration = "500"
/>
<alpha
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="300"
    />

After adding it to my adapter

public class WordAdapter extends ArrayAdapter<Word> {

    public WordAdapter(Context context, ArrayList<Word> words) {
        super(context, 0, words);

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if (listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_vocab, parent, false);
        }
        Word currentWord = getItem(position);

        TextView englishView =  listItemView.findViewById(R.id.englishWord);
        englishView.setText(currentWord.getmEnglishWord());

        TextView translatedView =  listItemView.findViewById(R.id.translatedWord);
        translatedView.setText(currentWord.getmTranslatedWordWord());

        TextView exampleView = listItemView.findViewById(R.id.exampleWord);
        exampleView.setText(currentWord.getmExample());

        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_left);
        convertView.startAnimation(animation);
        return listItemView;

    }
}

public class VocabActivity extends AppCompatActivity { private MediaPlayer mMediaPlayer;

private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        // Now that the sound file has finished playing, release the media player resources.
        releaseMediaPlayer();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.word_list);


    final ArrayList<Word> words = new ArrayList<Word>();
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));
    words.add(new Word("Abate", "To lessen the effect of something", "The storm has abated"));


    WordAdapter adapter = new WordAdapter(this, words);

    ListView listView = (ListView) findViewById(R.id.list);

    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {


            Word word = words.get(position);


            mMediaPlayer = MediaPlayer.create(VocabActivity.this, word.getAudioResourceId());

            // Start the audio file
            mMediaPlayer.start();

            // Setup a listener on the media player, so that we can stop and release the
            // media player once the sound has finished playing.


        }
    });

}

This is the activity I'm trying to launch, just a simple one with some textviews and an mp3 file that I'll add later

Noor
  • 51
  • 1
  • 6
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM Mar 23 '18 at 17:56
  • Your `convertView` is null for first time . Make it right . – ADM Mar 23 '18 at 17:58
  • show us the activity code – Muneerah Rashed Mar 23 '18 at 18:30
  • @MuneerahRashed I edited it in – Noor Mar 23 '18 at 19:47
  • @ADM The activity opens when I remove the lines for the animation. Would it open if the convertView was null? if so how could I change that? – Noor Mar 23 '18 at 19:48
  • Just remove listItemView and use convertview in it's place. It will work . – ADM Mar 23 '18 at 19:52
  • @ADM Oh my, thank you so much. I'm still not sure why it was working without the animation but gave me NullPointerException when I added it in. I would be very grateful if you explained that to me. – Noor Mar 23 '18 at 20:00
  • Undo the code and debug each step inside getView .you will get it. – ADM Mar 23 '18 at 20:01
  • @ADM I understand now, feels so good – Noor Mar 23 '18 at 21:09

1 Answers1

0

I suggest , switch your code from using list view to recycle view here a lot of amazing resources to animate your list :

How to Animate Addition or Removal of Android ListView Rows

http://frogermcs.github.io/instamaterial-recyclerview-animations-done-right/

and if you still want to fix this bug , add more details about your layout , also provide the line of the error and the class

Muneerah Rashed
  • 313
  • 3
  • 13