0

I'm triying to load a listview with an AsyncTask, but when the task is complete (onPostExecute method) and set the adapter to the listview the listview's height does not adjust to the items in it.

While the task is in doInBackground method a ProgressBar is shown. After, when the task is complete the ProgressBar is hidden and the ListView is loaded with the AsyncTask results.

My problem is the listview's height which does not show all the items in it.

Here is my code:

The listview declaration:

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Comments"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#262023"
                android:textStyle="bold" />

            <ProgressBar
                android:id="@+id/pbComentarios"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center" />

            <ListView
                android:id="@+id/lvComentarios"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:smoothScrollbar="true" >
            </ListView>

             <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#bbb" />

        </LinearLayout>

the item list template:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent" >

    <ImageButton
        android:id="@+id/ibUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_menu_my_calendar" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tvUsername"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="5dp"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvFechacomentario"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#aaa" />
        </LinearLayout>

        <TextView
            android:id="@+id/tvComentario"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000" />
    </LinearLayout>
</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000" />

the onPostExecute method:

protected void onPostExecute(ArrayList<Comentario> comentariosList) {
        pbComentarios.setVisibility(View.GONE); // hiding progressbar
        if( comentariosList != null ){
            comentarios = comentariosList;
            icAdapter.setComentarios(comentarios); // setting the arraylist to adapter which extends BaseAdapter
            lvComentarios.setAdapter(icAdapter); // setting adapter to listview
            lvComentarios.setDividerHeight(0);
            lvComentarios.refreshDrawableState(); 
            icAdapter.notifyDataSetChanged();
        }
    }

Can you help me please?, i tried all the listview's xml attributes but it seems nothing works.

Thanks in advance.

2 Answers2

1

Try this instead:

<ListView
        android:id="@+id/lvComentarios"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:smoothScrollbar="true" >

EDIT: Try this:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Comments"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#262023"
            android:textStyle="bold" />

        <ProgressBar
            android:id="@+id/pbComentarios"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <ListView
            android:id="@+id/lvComentarios"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:smoothScrollbar="true" />

         <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#bbb" />

    </LinearLayout>

Also, make sure that any higher up views have the height as "match_parent". Otherwise the ListView will not occupy the entire space as needed. Play around with either using "match_parent" or 0 with a weight of "1" (as above).

Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
  • it didn't work neither :(, the listview is still showing item and a half, thank you anyway. Another suggestion?? – ricardochv Jan 07 '13 at 04:23
  • Do you think you could take a screenshot and add it to your question? (and comment on here when you're done so I know). – Oleg Vaskevich Jan 07 '13 at 04:32
  • i tried to add a picture in the question itself but stackoverflow's policies did not allow me. Do you know another way to share a screenshot? – ricardochv Jan 07 '13 at 16:08
  • Ok It looks like this right now: http://i.imgur.com/ZwBH8.png And it is like i want it looks like: http://i.imgur.com/AnCrr.png I don't know if is important but it is not the unique layout in this view, there is others before and after it. I got the second screenshot (like it should looks like) setting a fixed heigjht to the listview's layout parent but the item's content may change so it has to be dynamic. Thanks for help. – ricardochv Jan 07 '13 at 16:48
  • The problem is that the `ListView` isn't occupying the full space as needed... it needs to stretch to occupy and "eat" as much space as possible. See my edit. – Oleg Vaskevich Jan 07 '13 at 23:17
  • Did not work neither :(.... and i don't know why the listview does no scroll at least. – ricardochv Jan 08 '13 at 02:36
  • Basically your issue is similar to this one: http://stackoverflow.com/questions/2383847/android-layout-with-listview-and-buttons – Oleg Vaskevich Jan 08 '13 at 03:39
0

I get it!! the problem was that all the layouts was inside a ScrollView and this not allowed listview to scroll and take its height, y removed it and now everything works fine. Thanks a lot for your help.