Spinner object does not accept second input after process execution

I am working on a finance application where I fill in fiat currency using 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 case, I have two spinner objects that allow the user to select the desired currency. from

is the first spinner and the to

second spinner.

So far everything is working fine as expected. The problem occurs when I click the get button and perform 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 also displays the value, but if I try to change the spinner items a second time, it doesn’t reflect anything, that is, it says that I clicked on the spinner and a dropdown menu appeared, but I changed the value, but it won the 'Show item that is running the selection process and the dropdown button is closed, so the spinner items are fixed in 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");
        }
    }

      

+3


source to share


1 answer


I haven't tested all of your code, but I saw that you are calling

edittxt.setText("No Internet Connection");

on doInBackground()

, which is not correct.



You need setText

to postExecute()

who works for UI-Thread

.

Return usdeur

in doInBackground()

and setText

onpostExecute()

+1


source







All Articles