我有一个带有 TextView 和 Checkbox(带有按钮图像)的自定义 ListView,当单击 textview 时,隐藏的布局会针对该特定行展开,当单击复选框时,该特定行被选中进行删除。ClasCcastException
当我将视图标记为 CheckBox 的ViewHolder
类对象时,我得到了这个。这是我的代码,holder
OnCheckedChangeListener
CustomListView.class
public View getView(final int position, View convertView, ViewGroup parent) {
holder = null;
DataFields rowItems = (DataFields) getItem(position);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.home_field_row, null);
holder = new ViewHolder();
holder.dataFields = items.get(position);
holder.mName = (TextView) convertView
.findViewById(R.id.hmFieldName);
holder.mDeleteCheck = (CheckBox) convertView
.findViewById(R.id.deleteCheck);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mDeleteCheck.setTag(position);
holder.mName.setTag(position);
holder.mName.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
holder = (ViewHolder) ((View) v.getParent()).getTag(); //Works fine here
...
}
});
holder.mDeleteCheck
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
holder = (ViewHolder) ((View) buttonView.getParent()).getTag(); //throwing ClassCastException
...
}
});
holder.mName.setText(rowItems.getName());
holder.mDeleteCheck.setChecked(deletePositions[position]);
return convertView;
}
我希望我的问题很清楚。非常感谢任何形式的帮助或建议。谢谢 !
编辑
日志猫 -
03-09 12:08:45.789: E/AndroidRuntime(3661): FATAL EXCEPTION: main
03-09 12:08:45.789: E/AndroidRuntime(3661): java.lang.ClassCastException: java.lang.Integer
03-09 12:08:45.789: E/AndroidRuntime(3661): at com.inkers.forgetpassword.utility.CustomListArrayAdapter$1.onCheckedChanged(CustomListArrayAdapter.java:173)
03-09 12:08:45.789: E/AndroidRuntime(3661): at android.widget.CompoundButton.setChecked(CompoundButton.java:124)
03-09 12:08:45.789: E/AndroidRuntime(3661): at android.widget.CompoundButton.toggle(CompoundButton.java:86)
03-09 12:08:45.789: E/AndroidRuntime(3661): at android.widget.CompoundButton.performClick(CompoundButton.java:98)