I am trying to retrieve the selected item of a ListView when user clicks a button. I am using the below code in setOnItemClickListener:
lvequipments.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Object o = lvequipments.getItemAtPosition(i);
CustomerEquipmentView customerEquipmentView = (CustomerEquipmentView)o;
Product product = workOrderPresenter.getCustomerModel().getProduct(customerEquipmentView);
Toast.makeText(getBaseContext(), customerEquipmentView.getModelName(), Toast.LENGTH_LONG).show();
}
});
And the toast is successfully getting the model name from the object when I select different items in the ListView. Outside the ListView I have a button that saves some data from a different panel. I am attempting to retrieve the ListView item again here like so
public void saveToDatabase() {
Object o = lvequipments.getSelectedItem();
CustomerEquipmentView customerEquipmentView = (CustomerEquipmentView)o;
...
}
But o is null. Evaluating lvequipments.getSelectedItem() at a breakpoint during run also gives null. How do I successfully retrieve the selected item from the ListView from outside the onItemClick() method?