簡體   English   中英

未調用自定義ArrayAdapter中的getView

[英]getView from customized ArrayAdapter isn't called

由於某些原因,未調用我自定義的ArrayAdapter的getView(int位置,View convertView,ViewGroup父級),並且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 <...>

編輯:這是我的布局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>

它被調用,只返回一個空值。 對於第一個getView調用,ConvertView為null,因此您必須對某些內容進行膨脹(即使它不是您的第一個位置,也無法保證它不為null),並且您似乎並未膨脹任何東西。

嘗試這個:

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

其中R.layout.yourlayout是您要用來填充列表的布局。

您是否嘗試過實際上為視圖充氣而不是使用轉換視圖。 僅當在陣列適配器內填充至少一個視圖時,Convertview才會為null

    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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM