不调用来自自定义ArrayAdapter的getView

时间:2014-10-23 17:05:20

标签: java android android-listview android-arrayadapter

由于某些原因,我的自定义ArrayAdapter的getView(int position,View convertView,ViewGroup parent)未被调用,android显示一个空列表。我已经覆盖了getCount()

public class CEL_ElementsFromXMLArrayAdapter extends ArrayAdapter<LinkedHashMap<RoomItem, LinkedHashMap<ItemType, List<RoomDamage>>>> {

private Context context;
private int resource;
private List<LinkedHashMap<RoomItem, LinkedHashMap<ItemType, List<RoomDamage>>>> elementsFromXMLList;

public CEL_ElementsFromXMLArrayAdapter(Context context, int resource,
        List<LinkedHashMap<RoomItem, LinkedHashMap<ItemType, List<RoomDamage>>>> elementsFromXMLList) {
    super(context, resource, elementsFromXMLList);

    this.context = context;
    this.resource = resource;
    this.elementsFromXMLList = elementsFromXMLList;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final int positionItem = position;
    View view;
    LayoutInflater viewInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView != null)
        view = convertView;
    else
        view = viewInflater.inflate(R.layout.etat_piece_item, parent, false);
    {...}
    return view;
}

@Override
public int getCount() {
    return this.elementsFromXMLList.size();
}

这就是我设置适配器的方式:

CEL_ElementsFromXMLArrayAdapter cel_ElementsFromXMLArrayAdapter = new CEL_ElementsFromXMLArrayAdapter(mContext, R.layout.etat_piece_item, elementsFromXMLLinkedHashMap);
            listViewElements.setAdapter(cel_ElementsFromXMLArrayAdapter);

我从昨天开始寻找解决方案,我已经阅读了StackOverflow上的所有帖子,但没有一个与我的问题相符。所以我想知道,如果它可以来自LinkedHashMap&lt; ...&gt;

编辑:这是我的布局R.layout.etat_piece_item

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mis2_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dip"
    android:background="@color/white"
    android:orientation="horizontal"
    android:weightSum="12" >

    <ImageView
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

    <TextView
        android:id="@+id/descriptionElement_TextView"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:clickable="true"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:textColor="@color/black1"
        android:textSize="@dimen/mis_item_size" />

    <ImageView
        android:id="@+id/stroke_dim"
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

    <Spinner
        android:id="@+id/typeElement_Spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_weight="2.5"
        android:background="@drawable/spinner_ab_default"
        android:clickable="true"
        android:paddingLeft="5dp"
        android:paddingTop="10dp"
        android:textSize="@dimen/mis_item_size"
        android:textStyle="normal" />

    <ImageView
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

    <EditText
        android:id="@+id/quantiteElement_EdiText"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:inputType="number"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:singleLine="true"
        android:textColor="@color/black1"
        android:textSize="@dimen/mis_item_size" />

    <ImageView
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

    <TextView
        android:id="@+id/etatElement_TextView"
        style="@android:style/Widget.Holo.Light.EditText"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_weight="1.5"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:onClick="callChooseEtat"
        android:paddingLeft="5dp"
        android:paddingTop="10dp"
        android:textStyle="normal" />

    <ImageView
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

    <TextView
        android:id="@+id/ent_obervations_val"
        style="@android:style/Widget.Holo.Light.EditText"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="2"
        android:clickable="true"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:singleLine="true"
        android:textColor="@color/black1"
        android:textSize="@dimen/mis_item_size" />

    <ImageView
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

    <LinearLayout
        android:id="@+id/imageLayout"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:gravity="center"
        android:onClick="callCameraInList" >

        <ImageView
            android:id="@+id/photoElement_ImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:contentDescription="@string/img"
            android:src="@drawable/photo_img" />
    </LinearLayout>

    <ImageView
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

    <EditText
        android:id="@+id/trouElement_EditText"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:hint="@string/zero"
        android:inputType="number"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:singleLine="true"
        android:textColor="@color/black1"
        android:textSize="@dimen/mis_item_size" />

    <ImageView
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

    <TextView
        android:id="@+id/actionElement_TextView"
        style="@android:style/Widget.Holo.Light.EditText"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center"
        android:paddingBottom="5dp"
        android:paddingTop="5dp"
        android:singleLine="true"
        android:textColor="@color/black1"
        android:textSize="@dimen/mis_item_size" />

    <ImageView
        android:layout_width="@dimen/stroke_dim"
        android:layout_height="fill_parent"
        android:background="@color/light_line"
        android:contentDescription="@string/img" />

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

它被调用,它只返回一个空值。对于第一个getView调用,ConvertView为null,所以你必须给某些东西充气(即使它不是你的第一个位置,也不能保证它不是空的)你似乎没有膨胀任何东西。

试试这个:

if(convertView != null)
  view = convertView;
else
  view = viewInflater.inflate(R.layout.yourlayout, parent, false);

R.layout.yourlayout是您希望用。填充列表的布局。

答案 1 :(得分:0)

您是否尝试实际膨胀视图而不是使用转换视图。 仅当在数组适配器

中填充至少一个视图时,Convertview才不为null

答案 2 :(得分:0)

    public class CEL_ElementsFromXMLArrayAdapter extends ArrayAdapter<LinkedHashMap<RoomItem, LinkedHashMap<ItemType, List<RoomDamage>>>> {

    private Context context;
    private int resource;
    private List<LinkedHashMap<RoomItem, LinkedHashMap<ItemType, List<RoomDamage>>>> elementsFromXMLList;

    public CEL_ElementsFromXMLArrayAdapter(Context context, int resource,
            List<LinkedHashMap<RoomItem, LinkedHashMap<ItemType, List<RoomDamage>>>> elementsFromXMLList) {
        super(context, resource, elementsFromXMLList);

        this.context = context;
        this.resource = resource;
        this.elementsFromXMLList = elementsFromXMLList;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final int positionItem = position;
        View view = convertView;
        View v = (View)LayoutInflater.from(context).inflate(your layout,parent, false);
        // this will make a list item from your layout then insert your data into it's child views

        return v;

    }

    @Override
    public int getCount() {
        return this.elementsFromXMLList.size();
    }