+ Reply to Thread
Results 1 to 2 of 2

Thread: Updating gridview multiple times after x seconds

  1. #1

    Updating gridview multiple times after x seconds

    I am having a problem updating the view in android every x seconds , i wrote the below code :
    Code:
    public void play() {
        while (playing) {
            getAndProcessInput();
            updateGui();
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
            }
        }
    }
    
    private void updateGui() {
        gui.setPlayer(x, y);
        // ...
    }
    Code:
    GameActivity.java:
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        GridView gridview = (GridView) findViewById(R.id.GridView1);
        TextAdapter = new TextAdapter(this);
        gridview.setAdapter(textAdapter);
    
        GameController c = new GameController();
    
        // here, the game loop shoud start.
        // what i tried:
    
        // new Thread(c).start();   <---- causes crash
    
        // c.play();     <---- causes view to freeze until game loop is done
    
        this.runOnUiThread(c);  <-- causes view to freeze until game loop is done
    }
    TextAdapter.java:
    Code:
    public class TextAdapter extends BaseAdapter implements IGui {
    
        private final Context context;
        private final String[] texts;
    
        public TextAdapter(Context context) {
            this.context = context;
            texts = new String[height * width];
            for (int i = 0; i < texts.length; i++) {
                texts[i] = " ";
            }
        }
    
        public int getCount() {
            return height * width;
        }
    
        public Object getItem(int position) {
            return null;
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            if (convertView == null) {
                tv = new TextView(context);
                tv.setLayoutParams(new GridView.LayoutParams(25, 25));
            } else {
                tv = (TextView) convertView;
            }
    
            tv.setText(texts[position]);
            return tv;
        }
    
        @Override 
        public void setPlayer(int x, int y) {
            texts[width * y + x] = "X";
        }
    }

  2. #2
    Do anything you want on your thread:
    Code:
       new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {
                            calculateGameChangesHere();
                            handler.sendEmptyMessage(SUCCESS);
                        }
                        catch (Exception e)
                        {
                            handler.sendEmptyMessage(FAILURE);
                        }
                    }
                }).start();
    When your data is ready, tell the handler to put it in a view and show it:
    Code:
    protected Handler handler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                if (msg.what == SUCCESS)
                {
                    setCalculatedDataToaView(); // the data you calculated from your thread can now be shown in one of your views.
                }
                else if (msg.what == FAILURE)
                {
                    errorHandlerHere();//could be your toasts or any other error handling...
                }
            }
        };
    This goes to everything that requires heavy processing/networking that shouldn't block your UI thread.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts