我已经覆盖了我的自定义 BaseAdapter 的方法 getView 来填充 Gridview:
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout relativeLayout;
if(convertView == null){
relativeLayout = (RelativeLayout)getItem(position);
} else{
relativeLayout = (RelativeLayout)convertView;
}
return relativeLayout;
}
但是一直以来,我都得到了这个异常,但我不明白为什么会得到它... getItem(position) 应该返回一个 Rectangle,它是一个自定义的 RelativeLayout。你能帮我么?
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
编辑:
我已经有一个自定义的 RelativeLayout,如下所示:
在我的getView中,我得到了我在arraylist中的这些矩形之一。这不是吗?
public class Rectangle extends RelativeLayout{
public Rectangle(Context context, int dnd_drag_button_while_dragging, String text) {
super(context);
this.context = context;
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
this.setLayoutParams(params);
ImageView image = new ImageView(context);
image.setBackgroundResource(dnd_drag_button_while_dragging);
image.setLayoutParams(params);
img = dnd_drag_button_while_dragging;
LayoutParams tParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
TextView textView = new TextView(context);
textView.setText(text);
textView.setLayoutParams(tParams);
this.addView(image);
this.addView(textView);
}