我正在构建货币转换器(从API获取,动态填充列表以创建可用货币,选择两种货币并返回费率。这似乎是通行权的权利。)
我一直试图让Spinner工作,但是尽管我付出了最大努力,但当我从列表中选择一个值时,我似乎无法让听众做任何事情。当我点击一个值时,日志会返回一些内容(见下文),但我不知道它的含义,以及为什么它没有返回我想要的System.out.println("yay")
电话。
D / OpenGLRenderer:0xa7040980上的endAllActiveAnimators (DropDownListView),句柄为0x95c21270"。
数组适配器正在从ArrayList<string>
我从API和JSON
对象获取的spinner列表中正确填充。根据我在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;
}
});
}
}
答案 0 :(得分:0)
您没有为适配器设置下拉视图。在setOnItemCLickListener
currencyOneSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
由于您使用AsyncTask
,因此每次更新列表时都不要忘记调用以下行。
arrayAdapter.notifyDataSetChanged();
注意:您必须全局初始化适配器。