Spinner1的代码无法正常工作,因为我不明白为什么它不起作用。所以我不明白为什么它不起作用。
package com.example.keshav.szdxfcbjnm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
//Creation of Two spinnners
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1); //This spinner has the probem
ArrayAdapter<CharSequence> adapr = ArrayAdapter.createFromResource(this,
R.array.pets_array, android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapr);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner1.setOnItemSelectedListener(this);
spinner.setOnItemSelectedListener(this);
Spinner spin = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
//maybe the probem occurs here
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.getId()) {
case R.id.spinner:
String s = String.valueOf(parent.getItemAtPosition(position));
EditText text = (EditText) findViewById(R.id.editText);
text.setVisibility(View.INVISIBLE);
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
if (s.equals("Mars")) {
text.setVisibility(View.VISIBLE);
break;
}
//Problem Lies Here
case R.id.spinner1:
String d = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(this, d, Toast.LENGTH_SHORT);
break;
default:
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
我是Android新手开发者,所以我为错误的命名道歉。使用此代码只有微调器的吐司正在工作。我想知道是否有人可以告诉我为什么,并告诉我如何纠正它。
答案 0 :(得分:1)
在spinner1
案例中,您未在吐司上致电show()
。改变这一行:
Toast.makeText(this, d, Toast.LENGTH_SHORT);
到此:
Toast.makeText(this, d, Toast.LENGTH_SHORT).show();