Ahmed Sedik Ahmed Sedik - 1 year ago 88
Java Question

How to iterate through custom ArrayList to move to the next item when a button is clicked

i am new to android and even programming its been only 3 weeks
i am trying to build an app for my german lesson through showing pics and playing audio.
my problem now, i have a list view which displays a small pic and a word and then i made an onItemClickListener for this list view and it displays items form and arraylist of type Word(a class made for the Constructor and get methods for image resources and audio resources and a String for the word it self)

anyway i managed when item clicked on the itemlist it open popup layout that displays image and a play button.

then i wanted to add next and prev buttons to go for next image and audio instead of closing the popup and click on the wanted item again But with no luck at all.

i will try to post most important code related

public class Lesson1Words extends AppCompatActivity {

private MediaPlayer mMediaPlayer;

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


protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

final ArrayList<Word> lessone1Words = new ArrayList<>();

lessone1Words.add(new Word("vernünftig", R.drawable.vernunftig, R.raw.l01_vernunftig));
lessone1Words.add(new Word("großzügig", R.drawable.groszugig, R.raw.l01_grosszugig));
lessone1Words.add(new Word("fair", R.drawable.fair, R.raw.l01_fair));
lessone1Words.add(new Word("intelligent/klug", R.drawable.intelligent, R.raw.l01_intelligent));
lessone1Words.add(new Word("frech", R.drawable.frech, R.raw.l01_frech));
lessone1Words.add(new Word("kreativ", R.drawable.kreaiv, R.raw.l01_kreativ));
lessone1Words.add(new Word("sparsam", R.drawable.sparsam, R.raw.l01_sparsam));
lessone1Words.add(new Word("realistisch", R.drawable.realistisch, R.raw.l01_realistisch));
lessone1Words.add(new Word("hübsch", R.drawable.hubsch, R.raw.l01_hubsch));
lessone1Words.add(new Word("aufmerksam", R.drawable.aufmerksam, R.raw.l01_aufmerksam));

final WordAdapter adapter = new WordAdapter(this, lessone1Words, R.color.lesson_color);
final ListView listView = (ListView) findViewById(R.id.word_list);

assert listView != null;

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

final Word word = lessone1Words.get(position);

//open Popup layout for the image but still can't iterate through the arraylist
LayoutInflater layoutInflater
= (LayoutInflater) getBaseContext()

View popupView = layoutInflater.inflate(R.layout.popup, null);

final PopupWindow popupWindow = new PopupWindow(
// when touch outside the Popup layout it dismiss it
popupWindow.setBackgroundDrawable(new BitmapDrawable());

//change image in ImageView of Popup.xml to current image in the array

final ImageView imageBig = (ImageView) popupView.findViewById(R.id.popimage);


ImageView playSound = (ImageView) popupView.findViewById(R.id.play_image);
playSound.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

//responsible for the playback of the audio according to the arraylist index
mMediaPlayer = MediaPlayer.create(Lesson1Words.this, word.getAudioResourceId());
mMediaPlayer.setVolume(0.5f, 0.5f);

popupWindow.showAtLocation(listView, Gravity.CENTER, 0, 0);

ImageView nextImage = (ImageView) popupView.findViewById(R.id.next_image);

nextImage.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

// cant go for the nextimage or audio


Answer Source

When creating popup window, set a tag with position value


In the onClick function call

int pos = imageBig.getTag()+1;
WordNext = getPosition(position);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download