В этом приложении каждый счетчик используется для вызова API.Теперь счетчик 1 предназначен для монеты (крипто), а 2 - для валюты (доллары США, иены и т. Д.), В которой должна отображаться монета.Блесна 1 работает отлично!Но Spinner 2 не будет обновлять TextView.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPriceTextView = findViewById(R.id.priceLabel);
Spinner spinner1 = findViewById(R.id.currency_spinner);
Spinner spinner2 = findViewById(R.id.coin_spinner);
// Create an ArrayAdapter using the String array and a spinner layout
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(this, R.array.currency_array, R.layout.spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.coin_array, R.layout.spinner_item);
// Specify the layout to use when the list of choices appears
adapter1.setDropDownViewResource(R.layout.spinner_dropdown_item);
adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
// TODO: Set an OnItemSelected listener on the spinner
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("Bitcoin-Ticker", "" + parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), "Spinner has been set with listener " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
Log.d("Bitcoin-Ticker", "Final url1 is: " + parent.getItemAtPosition(position).toString());
finalUrl1 = parent.getItemAtPosition(position).toString();
update();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.d("Bitcoin-Ticker", "No item selected.");
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("Bitcoin-Ticker", "" + parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), "Spinner has been set with listener" + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
Log.d("Bitcoin-Ticker", "Spinner choice is: " + parent.getItemAtPosition(position));
String spinItem = parent.getItemAtPosition(position).toString();
finalUrl2 = parent.getItemAtPosition(position).toString();
if (spinItem.equals("BTC")) {
finalUrl2 = BASE_URL1 + finalUrl1;
} else {
finalUrl2 = BASE_URL2 + finalUrl1;
}
Log.d("Bitcoin-Ticker", "Final url2 is: " + finalUrl2);
update();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}