user2361174
user2361174

Reputation: 1932

Android GridView and threading wait

I have a method playSoE() that performs an operation on each view and updates the ArrayAdapter and thus GridView after each operation. I want to do is have the code wait for 1 second after the update is done, and then perform the operation on the next view. For example:

public class MainActivity extends Activity{
    public void playSoE(){
        for(int i = 0; i < primesLE; i++) //iterates to each view
            mAdapter.setPositionColor(index, 0xffff0000 + 0x100 * index); //operation performed on view
            mAdapter.notifyDataSetChanged(); //updates adapter/GridView

            //Code that waits for one second here

        }
}

I have tried many threading APIs but none of them seem to work, they've either froze up the application for primesLE seconds and shown all the operations at the end, skip the second wait and just performed all the operations at once, or gotten an Exception related to concurrency.

Upvotes: 2

Views: 103

Answers (2)

Kevin Krumwiede
Kevin Krumwiede

Reputation: 10298

Assuming the update itself is quick, you should not be creating additional threads at all. Create a Handler and use postDelayed(...) to time your updates in the UI thread.

Upvotes: 1

Delaled
Delaled

Reputation: 9

You may have to take a look at AsyncTask.

Put the wait code at the doInBackground() and then the following that affects the visual on the onPostExecute()

All you do on doInBackground() will not freeze your application.

Upvotes: 0

Related Questions