0

I am struggling to go through each element of an Array at each button click, I can make a counter increase or decrease at each button click but I want to use for loop or while loop to do count or to go through each element of array.

Here is my code:

b.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          for (j = 0; j < 3; j++) {
              d = String.valueOf(amp[j]);

              Toast.makeText(MainActivity.this, d, Toast.LENGTH_LONG).show();
          }//outer
      }
});

I also declared a global array variable int[] amp = {4,8,7};

The problem is the loop prints each element at button click but what I want is on first click print first element of array, and if I click button again then print second element of array and so on.

1
  • you can create variable like int numberOfClicks and according to its value, print the array Commented Apr 6, 2017 at 17:12

2 Answers 2

1

You could use something like:

int counter = 0;

b.setOnClickListener(new View.OnClickListener() {
     @ Override public void onClick(View v) {
            d = String.valueOf(amp[counter]);
            Toast.makeText(MainActivity.this, d, Toast.LENGTH_LONG).show();
            counter++;
            if(counter>amp.length)
                counter=0;
    }
});
Sign up to request clarification or add additional context in comments.

4 Comments

many thanks Mohammed Bakr Sikal but i know that way but i need to use index number of forloop or while loop for instance i want my array list printed according to my index e.g i have array int amp[]={5,3,7} i want to print each element at one time but using index value of loop e.g for(int i=0i<amp.lemgth;i++){code here to print amp[index of i]}because i nedd to know which index is created i need this information to know element index of then i can clear that index instead of hardcoding position
Edit your post with the intended input and outputs.
ok i want to create marker on each click to google map, then i can tackle individual marker to remove them if i use index of loop, but how can i remove individual marker from google map without hardcoding, so i like to get result of element of array but using while or do or for loop so i need to use index of loop
The counter I suggested in my post is acting like an manual index of a loop. You can use map.clear() then add your new marker. Or, an other alternative would be to insert all your markers at the beginning, hide them (marker.setVisible(false);), then onClick, you are just making the marker with that index visible.
0

You might not need a for loop or while loop to do this. It can be accomplished simply by using an index variable and an if/else statement. Here, index is a member variable initialized to 0.

b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(index < amp.length){ 

                d = String.valueOf(amp[j]);
                Toast.makeText(MainActivity.this, d, Toast.LENGTH_LONG).show();

                // Increment index until we hit the last element
                index++;

            }else {
                // You can reset the index here so it starts at 0 once again
                index = 0;
            }
        }
});

3 Comments

thanks 11m0 ok i will tell you why i need loop.. because i want to create a list of LAtLing on each click a marker will b add to google map then on click on each markeri want to be able to removed, i think it is not possible to remove individual markers unless you have index of loop e.g if i say on index of element add marker then io be able to remove marker at index of loopp aswel
what i want to do i want to be able to add marker at position loop then i can ude marker.remove at position loop aswell without knowing what index it is done automaticaly
will make new thread to ask for google map thanks for youar answera

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.