我有一个带有四个微调器的活动,这些微调器是从数据库中填充的。您可以选择不同车辆的属性。让我们说我们可以选择品牌,然后马力等等。首先我想测试,如果我点击一个微调器时得到一个值,但我总是得到光标值,而不是我点击旋转器(例如本田,宝马)。我正在寻找几天,我也知道我需要像“getString()”或/和“selectedItem”这样的东西。但遗憾的是,我仍然无法解决问题。我想,例如点击微调项目“本田”,吐司信息应显示“本田”。 我希望有人可以告诉我,我必须把必要的代码放在哪里。我在光标内和下方试了一下。这是我的代码。谢谢: - )
public class ChooseCar extends Activity {
private Spinner BrandSpinner, HorsePowerSpinner, FuelSpinner, TireSpinner;
private Button btnSearch;
protected Cursor cursor;
protected ListAdapter adapter;
protected EditText searchText;
protected SQLiteDatabase db;
SQLiteDatabase myDB = null;
final static String MY_DATABASE_NAME = "car_directory";
final static String MY_DATABASE_TABLE = "cars";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.car_choice);
LoadBrandSpinner();
LoadHorsePowerSpinner();
LoadFuelSpinner();
LoadTireSpinner();
addListenerOnButton();
}
public void LoadBrandSpinner(){
myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
Cursor myCursor = myDB.rawQuery("SELECT _id, brand FROM " + MY_DATABASE_TABLE + ";", null);
startManagingCursor(myCursor);
BrandSpinner = (Spinner)findViewById(R.id.spinner1);
myCursor.moveToFirst();
SimpleCursorAdapter spinadapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
myCursor,
new String[] {"brand"},
new int[] {android.R.id.text1});
spinadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
BrandSpinner.setAdapter(spinadapter);
BrandSpinner.setPrompt(this.getResources().getString(R.string.ChooseBrand));
BrandSpinner.setAdapter(spinadapter);
BrandSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public void LoadHorsePowerSpinner(){// The same like the first spinner ;-)}
public void LoadFuelSpinner(){// The same like the first spinner ;-)}
public void LoadTireSpinner(){// The same like the first spinner ;-)}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The car is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
public void addListenerOnButton() {
BrandSpinner = (Spinner) findViewById(R.id.spinner1);
HorsePowerSpinner = (Spinner) findViewById(R.id.spinner2);
FuelSpinner = (Spinner) findViewById(R.id.spinner3);
TireSpinner = (Spinner) findViewById(R.id.spinner4);
btnSearch = (Button) findViewById(R.id.searchButton);
btnSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ChooseCar.this,
"OnClickListener : " +
"\nBrand : " + String.valueOf(BrandSpinner.getSelectedItem()) +
"\nHorsePower : " + String.valueOf(HorsePowerSpinner.getSelectedItem()) +
"\nFuel : " + String.valueOf(FuelSpinner.getSelectedItem()) +
"\nTire : " + String.valueOf(TireSpinner.getSelectedItem()),
Toast.LENGTH_SHORT).show();
}
});
}}
答案 0 :(得分:0)
将MyOnItemSelectedListener
设置为微调器的OnItemSelectedListener
是正确的。
在onItemSelected
的{{1}}方法中,您应该可以致电MyOnItemSelectedListener
,它会为您提供所选品牌的parent.getSelectedItem()
对象。
获得Cursor
后,请执行
Cursor
检索汽车品牌。
答案 1 :(得分:0)
appCompatSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(context, String.valueOf(((AppCompatTextView) view).getText()), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(context, "NothingSelected!", Toast.LENGTH_SHORT).show();
}
});