开发者

Unable to populate listview with custom rows when component is above listview

I'm having issues with a ListView using custom rows that are loaded from a database.

If, for the list screen, I place a button above the ListView, no visible rows appear in the listview.

However as soon as I remove the button, everything works fine. I want the button (or any other component) to appear above to make it more user friendly. Attached are the two code samples below.

This is the XML file of the ListView Activity that works:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/real_red_dark">
  <LinearLayout
    android:id="@+id/llMain"
    android:layout_height="fill_parent"
    android:background="@drawable/real_background"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:gravity="center">
        <ListView 
            android:id="@+android:id/list"      
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:开发者_运维技巧layout_below="@+id/llButton"
            android:layout_alignParentLeft="true" />        
        <TextView
            android:id="@+android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/no_sessions"
            android:textStyle="bold" 
            android:textSize="18dp" 
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>

However, if I have the Button added above it, it will not show whatsoever:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/real_red_dark">
  <LinearLayout
    android:id="@+id/llMain"
    android:layout_height="fill_parent"
    android:background="@drawable/real_background"
    android:layout_width="fill_parent"
    android:layout_gravity="center"
    android:gravity="center">
                <Button 
        android:id="@+id/btnSearch"
        android:layout_width="fill_parent"
        android:layout_height="35dip"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="3dip"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="15dip"
        android:text="Find Sessions"
        android:textStyle="bold" />
        <ListView 
            android:id="@+android:id/list"      
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/llButton"
            android:layout_alignParentLeft="true" />        
        <TextView
            android:id="@+android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="@string/no_sessions"
            android:textStyle="bold" 
            android:textSize="18dp" 
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>


You could try adding the button programmatically as a header view in the listview itself, rather than in the xml layout.

use listView.addHeaderView(View)

http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)


It is better if you use RelativeLayout than LinearLayout,it is also recommended by the android docs.Try to use android:layout_height value as "wrap_content" for ListView and TextView(which you may use to indicate for empty rows),it may help to you.


Something which doesn't look right but I'm assuming it's just a typo in the above...

android:id="@+android:id/list"

...there shouldn't be a + between @ and android:. Using @+ is for adding a new resource id of your own, i.e., @+id:. You're also doing the same thing for the TextView...

android:id="@+android:id/empty"

Another thing but not sure it's relevant is you're specifying...

android:layout_below="@+id/llButton"

...I doubt it's the problem as android:layout_below isn't valid for a LinearLayout (it's for RelativeLayout) but there isn't a Button with the id of llButton in your layout. If there was, the + would also be incorrect as you should be specifying an existing id.

Not sure if amending the above would fix things but it could just be that the layout inflation is coming out 'wrong' due to those issues.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜