Android ArrayAdapter,BaseAdapter没有调用getView()

时间:2014-08-20 14:18:15

标签: android listview android-listview android-arrayadapter baseadapter

我正在扩展ListFragment以显示从另一个类的AsyncTask中的Web服务器检索的数据。如果我在我的xml中的@android:id/list小部件上设置自定义高度(即400dp),则根本不会调用getView()。如果我将它设置为wrap_content,则会为列表项调用getView(),但列表项不会显示!即使列表项的xml指定了布局高度,listview似乎也不会扩展以适应项高度。当定义固定高度时,我可以看到列表的彩色背景,但没有数据。 getCount()永远不会返回0。

适配器类的一部分:

public class gAdapter extends ArrayAdapter<GroupObject> {
ArrayList<GroupObject> groups;

public gAdapter(Context context, int textViewResId, ArrayList<GroupObject> groups) {
    super(context, R.layout.groups_listview_row, groups);
    mInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
    con = context;
    this.groups = groups;
} 

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final int pos = position;
    View row = convertView;
    if(convertView == null) 
        row = mInflater.inflate(R.layout.groups_listview_row, parent, false);
    TextView name = (TextView) row.findViewById(R.id.groups_listview_name);
    //Set name
    try {
        name.setText( groups.get(position).groupName);
        Log.d("test", "Got name " + groups.get(position).groupName);
    } catch(Exception e) {
        e.printStackTrace();
        name.setText("Could not load name");
    }
        return row; 
    }

@Override
public int getCount() {
    Log.d("test", "Returning count " + (this.groups.size() != 0 ? this.groups.size() : 0));
    return this.groups.size() != 0 ? this.groups.size() : 0;
}

@Override
public GroupObject getItem(int position) {
    Log.d("test", "get item " + position);
    return this.groups.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

groups_listview_row.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" >

<ImageView
    android:id="@+id/groups_listview_imageview"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/avatar" />

<RelativeLayout
    android:id="@+id/RelativeLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/groups_listview_imageview"
    android:layout_alignTop="@+id/groups_listview_imageview"
    android:layout_toRightOf="@+id/groups_listview_imageview" >

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/groups_listview_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_weight="0.8"
            android:ellipsize="marquee"
            android:scrollHorizontally="true" 
            android:lines="1"
            android:marqueeRepeatLimit="marquee_forever"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/groups_listview_code"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="3dp"
            android:layout_marginRight="5dp"
            android:gravity="right|bottom"
            android:singleLine="true"
            android:text="zCode: 000000000" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/LinearLayout1"
        android:layout_gravity="bottom"
        android:gravity="bottom" >

        <TextView
            android:id="@+id/groups_listview_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="10dp"
            android:layout_weight="0.8"
            android:gravity="left|bottom"
            android:text="Location" />

        <TextView
            android:id="@+id/groups_listview_numMembers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginRight="10dp"
            android:layout_weight="0.2"
            android:gravity="right|bottom"
            android:text="0 Members" />
    </LinearLayout>
</RelativeLayout>

0 个答案:

没有答案