所以我要在android上创建货币转换应用程序,我有两个微调器,并且当第一个微调器已经选择了该选项时,我正试图从第二个微调器中隐藏该选项,例如如果我选择第二个从USD转换Spinner USD选项应该消失了,但我不知道如何隐藏我在Google上搜索的选项,大多数问题询问有关隐藏整个Spinner而不是
package com.currencyconverter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import static com.currencyconverter.globalData.amountToBeConvert;
import static com.currencyconverter.globalData.currencies;
import static com.currencyconverter.globalData.currencyFrom;
import static com.currencyconverter.globalData.currencyTo;
public class MainActivity extends AppCompatActivity {
public Button btn;
public EditText et_AmountToBeConverted;
public Spinner sp_CurrencyFrom;
public Spinner sp_CurrencyTo;
public TextView tv_ConvertedAmount;
public TextView tv_ConvertedCurrency;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declaring XML
btn = findViewById(R.id.button);
et_AmountToBeConverted = findViewById(R.id.et_AmountToBeConverted);
sp_CurrencyFrom = findViewById(R.id.sp_CurrencyFrom);
sp_CurrencyTo = findViewById(R.id.sp_CurrencyTo);
tv_ConvertedAmount = findViewById(R.id.tv_ConvertedAmount);
tv_ConvertedCurrency = findViewById(R.id.tv_ConvertedCurrency);
// Database work
databaseReference = FirebaseDatabase.getInstance().getReference("Rates");
databaseReference.keepSynced(true);
//Inserting string to spinners
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, currencies);
sp_CurrencyTo.setAdapter(adapter);
sp_CurrencyFrom.setAdapter(adapter);
//Spinner onseclectlistener
sp_CurrencyFrom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
currencyFrom = i;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
sp_CurrencyTo.getItemAtPosition((int) sp_CurrencyFrom.getSelectedItemId()).setEnabled(false);
sp_CurrencyTo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
currencyTo = i;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
//Convert!!
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Convert XML to data
amountToBeConvert = Double.parseDouble(et_AmountToBeConverted.getText().toString());
}
});
}
}
globalData
package com.currencyconverter;
public class globalData {
public static String[] currencies = {"AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYN", "BYR", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LVL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XDR", "XOF", "XPF", "YER", "ZAR", "ZMK", "ZMW", "ZWL" };
public static double rate;
public static double amountToBeConvert;
public static int currencyFrom;
public static int currencyTo;
public static String[] currencies2 = {"AED", "AFN", "ALL", "AMD", "ANG", "AOA", "ARS", "AUD", "AWG", "AZN", "BAM", "BBD", "BDT", "BGN", "BHD", "BIF", "BMD", "BND", "BOB", "BRL", "BSD", "BTC", "BTN", "BWP", "BYN", "BYR", "BZD", "CAD", "CDF", "CHF", "CLF", "CLP", "CNY", "COP", "CRC", "CUC", "CUP", "CVE", "CZK", "DJF", "DKK", "DOP", "DZD", "EGP", "ERN", "ETB", "EUR", "FJD", "FKP", "GBP", "GEL", "GGP", "GHS", "GIP", "GMD", "GNF", "GTQ", "GYD", "HKD", "HNL", "HRK", "HTG", "HUF", "IDR", "ILS", "IMP", "INR", "IQD", "IRR", "ISK", "JEP", "JMD", "JOD", "JPY", "KES", "KGS", "KHR", "KMF", "KPW", "KRW", "KWD", "KYD", "KZT", "LAK", "LBP", "LKR", "LRD", "LSL", "LTL", "LVL", "LYD", "MAD", "MDL", "MGA", "MKD", "MMK", "MNT", "MOP", "MRO", "MUR", "MVR", "MWK", "MXN", "MYR", "MZN", "NAD", "NGN", "NIO", "NOK", "NPR", "NZD", "OMR", "PAB", "PEN", "PGK", "PHP", "PKR", "PLN", "PYG", "QAR", "RON", "RSD", "RUB", "RWF", "SAR", "SBD", "SCR", "SDG", "SEK", "SGD", "SHP", "SLL", "SOS", "SRD", "STD", "SVC", "SYP", "SZL", "THB", "TJS", "TMT", "TND", "TOP", "TRY", "TTD", "TWD", "TZS", "UAH", "UGX", "UYU", "UZS", "VEF", "VND", "VUV", "WST", "XAF", "XAG", "XAU", "XCD", "XDR", "XOF", "XPF", "YER", "ZAR", "ZMK", "ZMW", "ZWL" };
}
编辑:
package com.currencyconverter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import static com.currencyconverter.globalData.amountToBeConvert;
import static com.currencyconverter.globalData.currencies;
import static com.currencyconverter.globalData.currencies2;
import static com.currencyconverter.globalData.currencyFrom;
import static com.currencyconverter.globalData.currencyTo;
public class MainActivity extends AppCompatActivity {
public Button btn;
public EditText et_AmountToBeConverted;
public Spinner sp_CurrencyFrom;
public Spinner sp_CurrencyTo;
public TextView tv_ConvertedAmount;
public TextView tv_ConvertedCurrency;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declaring XML
btn = findViewById(R.id.button);
et_AmountToBeConverted = findViewById(R.id.et_AmountToBeConverted);
sp_CurrencyFrom = findViewById(R.id.sp_CurrencyFrom);
sp_CurrencyTo = findViewById(R.id.sp_CurrencyTo);
tv_ConvertedAmount = findViewById(R.id.tv_ConvertedAmount);
tv_ConvertedCurrency = findViewById(R.id.tv_ConvertedCurrency);
// Database work
databaseReference = FirebaseDatabase.getInstance().getReference("quotes");
databaseReference.keepSynced(true);
//Inserting string to spinners
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, currencies);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, currencies2);
sp_CurrencyTo.setAdapter(adapter);
sp_CurrencyFrom.setAdapter(adapter2);
//Spinner onseclectlistener
sp_CurrencyFrom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
currencyFrom = i;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
adapter2.remove(currencies2[sp_CurrencyFrom.getSelectedItemPosition()]);
adapter2.notifyDataSetChanged();
sp_CurrencyTo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
currencyTo = i;
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
//Convert!!
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Convert XML to data
amountToBeConvert = Double.parseDouble(et_AmountToBeConverted.getText().toString());
}
});
}
}
答案 0 :(得分:0)
您无法从用于适配器的阵列中删除项目。
像这样声明它们:
ArrayList<String> currencies = new ArrayList<String>(Arrays.asList("AED", "AFN", "ALL", .......));
现在使用ArrayList,可以删除项目。
答案 1 :(得分:0)
Traceback (most recent call last):
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/app.py", line 90, in login
return render_template('login.html')
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/templating.py", line 135, in render_template
context, ctx.app)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask/templating.py", line 117, in _render
rv = template.render(context)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/templates/login.html", line 2, in top-level template code
{% import "bootstrap/wtf.html" as wtf %}
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 1, in top-level template code
{% block doc -%}
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 4, in block "doc"
{%- block html %}
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 20, in block "html"
{% block body -%}
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/flask_bootstrap/templates/bootstrap/base.html", line 23, in block "body"
{% block content -%}
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/templates/login.html", line 20, in block "content"
{{ frm.hidden_tag() }}
File "/Users/wouterlefebvre/PycharmProjects/OpportunityApp/venv/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'frm' is undefined