アンドロイド関連スピナーの実行方法は?アンドロイド関連スピナー
私はこのようにそれを行っている:
プライベートスピナーddlCountry、ddlCategory。 onItemSelectedを繰り返しますすなわち
@Override
public void onCreate(Bundle savedInstanceState) {
try {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.pnplandingpage);
ddlCountry = (Spinner) findViewById(R.id.ddlCountry);
ddlCategory = (Spinner) findViewById(R.id.ddlCategory);
// BindCountry(ddlCountry);
// ddlCountry
// .setOnItemSelectedListener(new OnCountryItemSelectedListener());
// ddlCategory
// .setOnItemSelectedListener(new
// OnCategoryItemSelectedListener());
// ArrayAdapter<CharSequence> adapter = ArrayAdapter
// .createFromResource(this, R.array.Country_array,
// android.R.layout.simple_spinner_item);
// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// ddlCountry.setAdapter(adapter);
ddlCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View pview,
int pos, long id) {
final String[] array = new String[] { "Category",
"5 Miles", "10 Miles", "15 Miles", "20 Miles",
"25 Miles", };
List<String> list = new ArrayList<String>();
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
PNPLandingPage.this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ddlCategory.setAdapter(dataAdapter);
Toast.makeText(
parent.getContext(),
"The Country is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
ddlCategory.setAdapter(null);
}
});
// BindCategory(ddlCategory);
// ddlCategory
// .setOnItemSelectedListener(new OnCategoryItemSelectedListener());
ddlCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View pview,
int pos, long id) {
final String[] array = new String[] { "Country",
"Country1", "Country2", "Country3", "Country4",
"Country5", };
List<String> list = new ArrayList<String>();
for (int i = 0; i < array.length; i++) {
list.add(array[i]);
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
PNPLandingPage.this,
android.R.layout.simple_spinner_item, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ddlCountry.setAdapter(dataAdapter);
// ddlCategory
// .setOnItemSelectedListener(new
// OnCategoryItemSelectedListener());
Toast.makeText(
parent.getContext(),
"The Category is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
ddlCountry.setAdapter(null);
}
});
} catch (Exception ex) {
Toast.makeText(getApplication(), "EXCEPTION :" + ex, 1000);
}
}
しかし、トーストは、毎回トリガーされます。何が私はあなたが任意のスピナーにsetOnItemSelectedListener()を設定したときに、それが発火するよう
トーストが表示されますか? – Richa
最初に親トーストが表示されてからカテゴリが表示されます。これはループとして続きます。 –