this is my first question here as a Android beginner. I am also not an native speaker, so please excuse my bad english skills. I want to implement an Android App for renting machines. To add a new contract, i created an AddVertragActivity.java
with a spinner. This spinner should be filled with data from a Room database. The problem is, that i can populate the spinner to view all items, but when I click on any item nothing happens. I discovered in debug mode, that the onItemSelected
method is never called.
I have read many other post here, but nothing solved my problem. Due to the fact, that no error message is shown, I have no idea what to do next. Every time I select an item in the dropdown list, there is a warning in the logfile:
D/OpenGLRenderer: endAllActiveAnimators on 0xe54e7a70 (DropDownListView) with handle 0xbc8d3a30
.
So I also do some research and only find a few answers, that didn't help me. Every time I start the activity there is also an warning about HiddenField. But I decreased the android version to Nougat and this warning was obsolet. So that didn't solved the problem either:
W/.viewpager_tes: Accessing hidden field Landroid/widget/AbsListView;->mIsChildViewEnabled:Z (greylist, reflection, allowed)
When you need some extra information, let me know. Thanks in advance for any advise.
public class AddVertragActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private Spinner spinnerB;
private BaumaschinenViewModel bViewModel;
private List<String> baumaschineList = new ArrayList<String>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_vertrag);
spinnerB = findViewById(R.id.spinnerBaumaschine);
bViewModel = new ViewModelProvider(this).get(BaumaschinenViewModel.class);
bViewModel.getAllBaumaschinen().observe(this, baumaschines -> {
for (int i = 0; i < baumaschines.size(); i++) {
baumaschineList.add(baumaschines.get(i).getMachineName());
System.out.println(baumaschines.get(i).getRowid());
}
});
ArrayAdapter<String> spinnerBaumaschinenAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, baumaschineList);
spinnerBaumaschinenAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerBaumaschinenAdapter.notifyDataSetChanged();
spinnerBaumaschinenAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerB.setAdapter(spinnerBaumaschinenAdapter);
spinnerB.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Baumaschine auswählen")) {
} else {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
You are missing a call to set the item listener that is the reason it is not been called when you clicked any item.
Do something like below. After setting adapter spinnerB.setAdapter(spinnerBaumaschinenAdapter);
spinnerB.setOnItemSelectedListener(this);
Try to use spinnerB.setOnItemClickListener
instead of spinnerB.setOnItemSelectedListener
.
Hope this helps. Thank you...
Kindly let me know whether it is working or not. Thank you...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.