Java onItemSelectedListener 和 Spinner 未返回任何内容

Java onItemSelectedListener and Spinner not returning Anything

我正在构建一个货币转换器(从 API 获取,动态填充列表以创建可用货币,select 两种货币和 return 汇率。这似乎是段落练习)。

我一直在努力让 Spinner 工作,但尽管我尽了最大努力,但当我 select 列表中的一个值时,我似乎无法让侦听器做任何事情。当我单击一个值时,日志会执行 return 某些操作(见下文),但我不知道这意味着什么,也不知道为什么它没有 returning 我想要的 System.out.println("yay") 调用.

D/OpenGLRenderer: endAllActiveAnimators on 0xa7040980 (DropDownListView) with handle 0x95c21270".

数组适配器正确地从 ArrayList<string> 我从 API 和 JSON 对象中获取微调器列表。根据我在 S.O 上找到的示例,侦听器代码看起来是正确的。到目前为止。

我想知道这是否是我的代码中的顺序问题?或者别的。

package com.timlee.currencymate;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

    public class MainActivity extends AppCompatActivity {

// public variables
    String uRLResultString = "";
    ArrayList <String> currencies = new ArrayList<String>();
    Spinner currencyOneSpinner;
    Spinner currencyTwoSpinner;

// get data from api
public class DownloadTask extends AsyncTask <String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        URL url;
        HttpURLConnection urlConnection = null;

        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection)url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);

            int data = reader.read();

            while (data != -1) {
                char current = (char) data;
                uRLResultString += current;
                data = reader.read();
            }

            return uRLResultString;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        //System.out.println(result);
        //if get current list then run this, else run something else
        getCurrencyList();

    }
}

//gets the list of currecies
public void getCurrencyList () {

    try {

        JSONObject jsonObject = new JSONObject(uRLResultString);
        String base = jsonObject.getString("base");
        String rates = jsonObject.getString("rates");
        currencies.add(base);
        System.out.println(rates);

        JSONObject jSonObj = new JSONObject(rates);

        Iterator<String> keys = jSonObj.keys();

        while (keys.hasNext()) {
            String key = (String)keys.next();
            currencies.add(key);
            if (jSonObj.get(key) instanceof JSONObject) {
            }
        }

        Collections.sort(currencies, String.CASE_INSENSITIVE_ORDER);

    } catch (JSONException e) {
        e.printStackTrace();
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    currencies = new ArrayList<String>();

    String currencyURL = "http://api.fixer.io/latest?base=AUD";
    DownloadTask task = new DownloadTask();
    task.execute(currencyURL);

    // apply Array Adapter and listener
    currencyOneSpinner = (Spinner)findViewById(R.id.spinnerCurrencyOne);
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,currencies);
    currencyOneSpinner.setAdapter(arrayAdapter);
    currencyOneSpinner.setOnItemSelectedListener (new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            System.out.println("yay");
            return;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            System.out.println("neyh");
            return;
        }
    });
}
}

您没有为适配器设置下拉视图。在 setOnItemCLickListener

上方添加此行

currencyOneSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

并且由于您使用的是 AsyncTask,请不要忘记在每次更新列表时调用以下行。

arrayAdapter.notifyDataSetChanged();

注意:您必须全局初始化适配器。