Search code examples
androidandroid-spinneryahoo-finance

Spinner object doesn't take second input once a process is being executed


I am working on a finance app where i am populating the exchange currecny value using the Yahoo Finance Api and here is my implementation

void currency_widget() {
    from.setAdapter(new CurrencyListAdapter(getActivity(), R.layout.country_list_item, recourseList));
    from.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String m = (String) Array.get(recourseList, i);
            String[] ma = m.split(",");
            Locale locale = new Locale("en_US", ma[1]);
            c2 = Currency.getInstance(locale);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });


    to.setAdapter(new CurrencyListAdapter(getActivity(), R.layout.country_list_item, recourseList));
    to.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String m = (String) Array.get(recourseList, i);
            String[] ma = m.split(",");
            Locale locale = new Locale("en_US", ma[1]);
            c1 = Currency.getInstance(locale);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });


}

public class connection extends AsyncTask {
    @Override
    protected Object doInBackground(Object... arg0) {
        connect();
        return null;
    }

    private void connect() {
        try {
            String as = c2 + "" + c1 + "=" + "X";
            // FxQuote ab=YahooFinance.getFx(FxSymbols.)
            FxQuote usdeur = null;
            try {
                usdeur = YahooFinance.getFx(as);
                edittxt.setText("" + usdeur);
            } catch (Exception e) {
                e.printStackTrace();
                edittxt.setText("No Internet Connection");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

In this I have two spinner object which allows the user to select their desired currency from is the first spinner object and to is the second spinner object.

This far everything works fine as expected. The problem arises when I click the get button and execute the operation

get.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new connection().execute(); //TODO Spinner won't take second time input
        }
    });

It display the value too, but if I try to change the spinner items for the second time it does not reflect anything i.e lets say I clicked on the spinner and the drop down menu appeared but and i changed the value but it won't show the item the selection process is done and the dropdown button is closed hence the spinner items fix to the first position

Solved

Changed the code as per the answer received and it worked

@Override
    protected Object doInBackground(Object... arg0) {
       // connect();
        String as = c2 + "" + c1 + "=" + "X";
       // FxQuote usdeur = YahooFinance.getFx(as);
        return YahooFinance.getFx(as);
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);
        try {
            edittxt.setText("" + o);
        } catch (Exception e) {
            e.printStackTrace();
            edittxt.setText("No Internet Connection");
        }
    }

Solution

  • I didnt checked all your code but i saw that you are calling

    edittxt.setText("No Internet Connection"); on doInBackground() which is wrong.

    You have to setText on postExecute() which runs on UI-Thread.

    Return usdeur on doInBackground() and setText on postExecute()