我有一个用自定义适配器填充的微调器。
用于将数据绑定到微调器的代码是
private void setOperatorSpinnerAdapter(Spinner spinner, String data) {
listOperator = new ArrayList<>();
try {
jsonObject = new JSONObject(data);
jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
model2 = new LiabModel();
model2.setName(jsonObject.optString("name"));
listOperator.add(model2);
}
} catch (JSONException e) {
e.printStackTrace();
}
adapter_liab = new CustomListAdapter_liab(LiabilitiesActivity.this, listOperator);
spinner.setAdapter(adapter_liab);
}
我的问题是项目选择。我收到错误
java.lang.ClassCastException:java.lang.Integer无法强制转换为Model.LiabModel
我的微调器项目点击代码是
spnr.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
try {
LiabModel model2 = (LiabModel) parent.getItemAtPosition(position);
liab_type = model2.getName();
}
catch (Exception e){
Log.e("exc",""+e);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我正在获取Class Cast异常 &#34; LiabModel model2 =(LiabModel)parent.getItemAtPosition(position);&#34;这条线。为什么会这样?
答案 0 :(得分:1)
您可以通过此
获得选定的值 LiabModel model2 = (LiabModel) spnr.getItemAtPosition(position);
或者是否可以访问listOperator
LiabModel model2 = listOperator.get(position);