微调器下拉列表setOnItemSelectedListener不工作-android
大家好,我一直在尝试连接这个微调器,但它不会调用选定的侦听器。这是我一直看到的,所以,每一个图特,但没有任何工作 *****************代码微调器下拉列表setOnItemSelectedListener不工作-android,android,events,spinner,Android,Events,Spinner,大家好,我一直在尝试连接这个微调器,但它不会调用选定的侦听器。这是我一直看到的,所以,每一个图特,但没有任何工作 *****************代码 public class QuoteActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener { public Spinner mCountrySpinner; public Spinner mStateSpinner; public S
public class QuoteActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public Spinner mCountrySpinner;
public Spinner mStateSpinner;
public Spinner mCitySpinner;
public Spinner mPayPointSpinner;
public Spinner mDeliveryCurrencySpinner;
public ArrayList<String> mCountryOptions = new ArrayList<>();
public ArrayList<String> mStateOptions = new ArrayList<>();
public ArrayList<String> mCityOptions = new ArrayList<>();
public ArrayList<String> mPayPointOptions = new ArrayList<>();
public ArrayList<String> mDeliveryCurrencyOptions = new ArrayList<>();
public String mSelectedCountry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote);
overridePendingTransition(R.anim.anim_slide_in_left, R.anim.anim_slide_out_left);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle(R.string.Calculate_a_Quote);
mCountrySpinner = (Spinner) findViewById(R.id.country_spinner);
mStateSpinner = (Spinner) findViewById(R.id.state_province_spinner);
mCitySpinner = (Spinner) findViewById(R.id.city_spinner);
mPayPointSpinner = (Spinner) findViewById(R.id.pay_point_spinner);
mDeliveryCurrencySpinner = (Spinner) findViewById(R.id.delivery_currency_spinner);
mCountrySpinner.setOnItemSelectedListener(this);
populateSpinnerData(WebApiManager.FAKE_AGENT_ID);
}
@Override
public void onStart(){
super.onStart();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.anim_slide_in_right, R.anim.anim_slide_out_right);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quote, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: // Respond to the action bar's Up/Home button
onBackPressed();
return true;
case R.id.quote_action:
Toast.makeText(this, "TODO: Get Quote", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
public void populateSpinnerData(String agentID){
getPayPointApiInfo(agentID, Utility.payPointDataType.countryType.toString(), null);
ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mCountryOptions);
mCountrySpinner.setAdapter(countryAdapter);
}
private void convertData(JSONObject data,String dataType) {
try {
String status = data.getString(WebApiManager.KEY_STATUS);
if (status.equals("Ok")) {
if(dataType.equalsIgnoreCase(Utility.payPointDataType.countryType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mCountryOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.stateType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mStateOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.cityType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mCityOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.payPointType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mPayPointOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.deliveryCurrencyType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mDeliveryCurrencyOptions.add(row.getString("value"));
}
}
}
} catch (JSONException e) {
Log.i("PendingQueue", e.getLocalizedMessage());
} catch (JsonSyntaxException jse) {
Log.i("PendingQueue", jse.getLocalizedMessage());
}
}
public void getPayPointApiInfo(String agentID, final String type, String country) {
WebApiManager.getPayPointInfo(this, agentID, type, country, new WebApiManager.OnResponseListener() {
@Override
public void onResponse(JSONObject response) {
Log.i(getClass().getSimpleName(), "Response = " + response.toString());
convertData(response, type);
}
}, new WebApiManager.OnErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i(getClass().getSimpleName(), "Error");
AlertDialog alert = new AlertDialog.Builder(QuoteActivity.this)
.setMessage(error.getCause().getMessage())
.setTitle(R.string.app_name)
.create();
alert.show();
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
公共类QuoteActivity扩展AppCompativity实现AdapterView.OnItemSelectedListener{
公共微调器McCountrySpinner;
公共微调器mStateSpinner;
公共纺纱机;
公共微调器MPayPoint微调器;
公共微调器mDeliveryCurrencySpinner;
public ArrayList mCountryOptions=new ArrayList();
public ArrayList mStateOptions=new ArrayList();
public ArrayList mCityOptions=new ArrayList();
public ArrayList mPayPointOptions=new ArrayList();
public ArrayList mDeliveryCurrencyOptions=new ArrayList();
公共字符串mSelectedCountry;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote);
覆盖转换(R.anim.anim\u滑入左,R.anim.anim\u滑出左);
ActionBar ActionBar=getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(真);
actionBar.setTitle(R.string.Calculate\u a\u Quote);
McCountrySpinner=(Spinner)findViewById(R.id.country\u Spinner);
mStateSpinner=(微调器)findViewById(R.id.state\u province\u微调器);
mCitySpinner=(微调器)findViewById(R.id.city\u微调器);
mPayPointSpinner=(Spinner)findViewById(R.id.pay\u point\u Spinner);
mDeliveryCurrencySpinner=(微调器)findViewById(R.id.delivery\u currency\u微调器);
mCountrySpinner.setOnItemSelectedListener(此);
populateSpinnerData(webapimager.false\u AGENT\u ID);
}
@凌驾
public void onStart(){
super.onStart();
}
@凌驾
恢复时公开作废(){
super.onResume();
}
@凌驾
public void onBackPressed(){
super.onBackPressed();
覆盖转换(R.anim.anim\u滑入\u右侧,R.anim.anim\u滑出\u右侧);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
case android.R.id.home://响应操作栏的Up/home按钮
onBackPressed();
返回true;
案例R.id.quote_行动:
Toast.makeText(这个“TODO:Get Quote”,Toast.LENGTH_SHORT.show();
打破
}
返回super.onOptionsItemSelected(项目);
}
public void populateSpinnerData(字符串agentID){
getPayPointApiInfo(agentID,Utility.payPointDataType.countryType.toString(),null);
ArrayAdapter countryAdapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项,mCountryOptions);
mCountrySpinner.setAdapter(countryAdapter);
}
私有void convertData(JSONObject数据,字符串数据类型){
试一试{
字符串状态=data.getString(webapimager.KEY\u状态);
如果(状态等于(“正常”)){
if(dataType.equalsIgnoreCase(Utility.payPointDataType.countryType.toString()){
JSONArray info=data.getJSONArray(webapimager.KEY\u CAPITAL\u RESULT);
对于(int i=0;i<LinearLayout
android:id="@+id/country_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="15dp">
<TextView
android:id="@+id/country_title_text"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:textAppearance="?android:textAppearanceMedium"
android:text="Country"/>
<Spinner
android:id="@+id/country_spinner"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content" />
</LinearLayout>
mCountrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "test "+position, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
private void convertData(JSONObject data,String dataType) {
try {
String status = data.getString(WebApiManager.KEY_STATUS);
if (status.equals("Ok")) {
if(dataType.equalsIgnoreCase(Utility.payPointDataType.countryType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mCountryOptions.add(row.getString("value"));
}
ArrayAdapter<String> countryAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mCountryOptions);
mCountrySpinner.setAdapter(countryAdapter);
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.stateType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mStateOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.cityType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mCityOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.payPointType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mPayPointOptions.add(row.getString("value"));
}
}
if(dataType.equalsIgnoreCase(Utility.payPointDataType.deliveryCurrencyType.toString())){
JSONArray info = data.getJSONArray(WebApiManager.KEY_CAPITAL_RESULT);
for(int i = 0; i < info.length();i++){
JSONObject row = info.getJSONObject(i);
if(row != null)
mDeliveryCurrencyOptions.add(row.getString("value"));
}
}
}
} catch (JSONException e) {
Log.i("PendingQueue", e.getLocalizedMessage());
} catch (JsonSyntaxException jse) {
Log.i("PendingQueue", jse.getLocalizedMessage());
}
}