[英]Inflate custom component into ArrayAdapter
I know how to inflate custom layout from XML, but how can I inflate custom component into ArrayAdapter? 我知道如何从XML扩展自定义布局,但是如何将自定义组件扩展到ArrayAdapter?
Here is my component: 这是我的组件:
public class MyComponent extends LinearLayout
{
private EditText mText;
private ImageView mImage;
public MyComponent(Context context, AttributeSet attrs)
{
super(context, attrs);
View.inflate(context, R.layout.cell_layout, this);
}
private void initComponent(){
mText = (EditText) findViewById(R.id.lats_field);
mImage = (ImageView) findViewById(R.id.flag_img);
}
public EditText getText()
{
return mText;
}
public void setText(EditText mText)
{
this.mText = mText;
}
public ImageView getImage()
{
return mImage;
}
public void setImage(ImageView mImage)
{
this.mImage = mImage;
}
}
And I would like to add it to ListView using ArrayAdapter. 我想使用ArrayAdapter将其添加到ListView。 How can I do it?
我该怎么做?
Create an xml and put inside it MyComponent: 创建一个xml并将其放入MyComponent中:
<com.package.MyComponent
android:layout_with="fill_parent"
android:layout_heigth="wrap_content"
/>
override the getView from your ArrayAdapter and when convertView is null inflate this layout, and assign it to convertView. 从ArrayAdapter重写getView,当convertView为null时,将此布局膨胀,并将其分配给convertView。 Otherwise you can define the constructor of LinearLayout that takes only the Context as parameter and, in inside the getView, when convertView is null you do
否则,您可以定义LinearLayout的构造函数,该构造函数仅将Context作为参数,并且在getView内,当convertView为null时,您可以执行
convertView = new MyComponent(mContext);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.