我一直在尝试使用自定义适配器为列表视图获取此自定义xml布局以永久工作(呈现)。没有错误被抛出,顶级textview只是拒绝渲染(我在Android设备管理器中检查)。 我还在getView函数中调试了它,并确保正确填充了对象列表,并且textview上的findviewbyid工作正常。
继承人xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dp"
android:background="@drawable/itembackground">
<ImageView
android:id="@+id/itemicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip" />
<TextView
android:id="@+id/itemtopLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/itembotLine"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/itemicon"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="16sp" />
<TextView
android:id="@+id/itembotLine"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/itemicon"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
</RelativeLayout>
继承人适配器
public class SpaceAdapter extends ArrayAdapter<SpaceItem>{
public SpaceAdapter(Context context, int resource, ArrayList<SpaceItem> items) {
super(context, resource, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null){
v = LayoutInflater.from(getContext()).inflate(R.layout.space_item, null);
}
SpaceItem item = getItem(position);
ImageView icon = (ImageView) v.findViewById(R.id.itemicon);
TextView name = (TextView) v.findViewById(R.id.itemtopLine);
TextView prop = (TextView) v.findViewById(R.id.itembotLine);
if (item != null){
icon.setImageDrawable(item.spaceItemIcon);
name.setText(item.spaceItemName);
prop.setText(item.spaceItemDescription);
}
else {
icon.setImageDrawable(getContext().getResources()
.getDrawable(android.R.drawable.btn_star, null));
name.setText("Default");
name.setText("kek");
}
return v;
}
}
这是项目
public class SpaceItem {
public Drawable spaceItemIcon;
public String spaceItemName;
public String spaceItemDescription;
public SpaceItem (Drawable ico, String name, String d){
spaceItemDescription = d;
spaceItemIcon = ico;
spaceItemName = name;
}
}