我正在尝试在用户点击按钮时检索ListView
的所选项目。我在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();
}
});
当我在ListView
中选择不同的项目时,toast成功从对象获取模型名称。在ListView
之外我有一个按钮,可以保存来自不同面板的一些数据。我试图在这里再次检索ListView
项目
public void saveToDatabase() {
Object o = lvequipments.getSelectedItem();
CustomerEquipmentView customerEquipmentView = (CustomerEquipmentView)o;
...
}
但o
为空。在运行期间在断点处评估lvequipments.getSelectedItem()
也给出null。如何从ListView
方法外部onItemClick()
成功检索所选项目?
答案 0 :(得分:1)