0

I have written the android application consisting of the main activity as an action bar, with 3 fragments, one fragment for each tab, for collecting and presenting some financial data. One of the fragment consists of the spinner for selection of the currency (USD, EUR, etc). When a particular currency is selected I draw a chart for it. In addition I would like to make some sort of prediciton game and ask user to guess a next day performance (rate) of the selected currency (UP, DOWN). For the prediction I use 2 radio buttons (UP, DOWN) in one radio group. Based on the user prediction for each currency I build some score for him using Sqlite to remember past predictions.

My program is as folowing. In order to handle a selection of radio button I use radio group setOnCheckedChangeListener. Based on user selection I make an update of Sqlite database. For spinner I use setOnItemSelectedListener and handle onItemSelected the user choice of a currency. onItemSelected method starts a new thread. The thread does two things: draw a chart for selected currency and ask Sqlite database if there is already some prediction made for the given currency. The thread build the message (from Sqlite record: UP, DOWN, NO_CHOICE) for the handler (registered on the fragment level) that fires (or should...) handleMessage method that for both radio buttons calls setChecked method relevant for already got message (it could be for both: false if no choice was made for a given currency).

The problem is that my radio button works only if I click them manually. I can not set them automatically based on the message provided. (the application does not generate any error). I dont know why.

Some distilled code is bellow.

//Spinner

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                currency= currencyList[arg2];

                new Thread(new Runnable(){
                    @Override
                    public void run() {
                        // some Sqlite query to get prediction used to build the message
                        Message msg = new Message();
                        msg.obj = prediction;
                        handler.sendMessage(msg);

                        graphView.getHandler().post(new Runnable(){
                        // some processign to build a chart for a given currency
                        }
                // some other stuff
                }).start();

// Handler

handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                super.handleMessage(msg);

                if (msg.obj.toString() == "NO_CHOICE") {
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                } else if (/* some other condition*/)
                    // some other code
                }               
            }   
        };
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
JackAW
  • 164
  • 2
  • 14

2 Answers2

0

Use ...new Handler(Looper.getMainLooper()) {... instead of just ...new Handler() {... - the Handler is being called from a different Thread, and only the main Thread can touch any of the Views

Ben Ezard
  • 463
  • 6
  • 18
0

As it was pointed out by Matteo Lovato (on Coursera platform) the if condition inside handleMessage method is not proper and should be: if (msg.obj.toString().equals("NO_CHOICE")). It solves the problem.

JackAW
  • 164
  • 2
  • 14