Could you please help me? The spinner I have created a spinner. When I click on it I can see a list of items but when I click on any of them, the drop-down many closes, and no element gets selected. I tried to change text color, and background but nothing helps. Any idea what could be a problem?
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
int quantity = 0;
TextView textView;
Spinner spinner;
ArrayList spinnerArraylist;
ArrayAdapter spinnerAdapter;
HashMap goodsMap;
String goodsname;
double price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
spinner = findViewById(R.id.spinner);
spinnerArraylist = new ArrayList();
spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, spinnerArraylist);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinnerAdapter.notifyDataSetChanged();
spinner.setAdapter(spinnerAdapter);
spinnerArraylist.add("guitar");
spinnerArraylist.add("drums");
spinnerArraylist.add("keybord");
goodsMap = new HashMap();
goodsMap.put("guitar", 500.0);
goodsMap.put("drums", 1500.0);
goodsMap.put("keybord", 1000.0);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
goodsname = spinner.getTransitionName().toString();
price = (double) goodsMap.get(goodsname);
TextView priceTextView = findViewById(R.id.textView);
priceTextView.setText("" + quantity + price);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
The spinner itself:
<Spinner
android:id="@+id/spinner"
android:theme="@style/mySpinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
style.xml
<resources>
<style name="mySpinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
<item name="android:textSize">12sp</item>
<item name="android:textColor">@color/black</item>
<item name="android:spinnerMode">dropdown</item>