执行过程后,Spinner 对象不会接受第二个输入

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

我正在开发一个财务应用程序,我在其中使用 Yahoo Finance Api 填充交换货币值,这是我的实现

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();
        }
    }
}

在此我有两个微调器对象,允许用户select他们想要的货币from是第一个微调器对象,to是第二个微调器对象。

到目前为止,一切都按预期正常工作。 点击get按钮执行操作出现问题

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

它也显示该值,但是如果我第二次尝试更改微调器项,它不会反映任何内容,也就是说,我点击了微调器,出现了下拉菜单,但我更改了值,但是它不会显示 selection 过程已完成且下拉按钮已关闭的项目,因此微调器项目固定在第一个位置

Solved

根据收到的答案更改了代码并且有效

@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");
        }
    }

我没有检查你所有的代码,但我看到你在打电话

edittxt.setText("No Internet Connection");doInBackground() 上是错误的。

您必须 setTextpostExecute() 上运行 UI-Thread

Return usdeur doInBackground()setText postExecute()