Android 在不使用布局充气器的情况下使用getView()
我希望在不使用xml布局的情况下填充项目,而希望在运行时创建视图。这是我的Android 在不使用布局充气器的情况下使用getView(),android,android-layout,android-edittext,classcastexception,android-adapter,Android,Android Layout,Android Edittext,Classcastexception,Android Adapter,我希望在不使用xml布局的情况下填充项目,而希望在运行时创建视图。这是我的getView()方法 public View getView(final int position, View convertView, ViewGroup parent) { EditText v ; if(convertView ==null) { v = new EditText(mContext); convertView = v;
getView()
方法
public View getView(final int position, View convertView, ViewGroup parent) {
EditText v ;
if(convertView ==null)
{
v = new EditText(mContext);
convertView = v;
convertView.setTag(R.string.edittext_child, v);
}
else
{
v = (EditText)convertView.getTag(R.string.edittext_child);
}
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 80));
v.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.custom_edit));
v.setEms(12);
v.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
v.setPadding(10, 0, 5, 0);
v.setHint(Ids[position]);
return convertView;
}
返回视图时出现此错误
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
现在很可能导入了LinearLayout布局参数,这是一个错误的导入。 这就是为什么你会 java.lang.ClassCastException:android.view.ViewGroup$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams
v.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 80));
因为您正在尝试将线性布局参数添加到AbsList布局,该布局应使用AbdList参数填充
这一行是错误的:
v、 setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,80))
将LayoutParams更改为:
AbsListView.LayoutParams
或更改您的导入:
import android.widget.AbsListView;
使用AbsListView.LayoutParams
v.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, 80));
您可以将
编辑文本
视为视图
,但不能反过来。我认为您需要使用LinearLayout.LayoutParams而不是ViewGroup.LayoutParams删除LayoutParams的导入行并导入LinearLayout.LayoutParams。它应该会起作用。