killer
killer

Reputation: 602

List view displaying only one record when Android scroll view is used

I am new to android and I am displaying a ListView getting data from a webservice. Below listview there are some other views/controls on the activity.Now the issue I am getting is that when the webservice returns more than one record controls below the listview are getting hidden at the bottom of window for that I tried to use scrollView in my android activity but the issue I am getting after using scroll view is that only one record is getting displayed in the listview. So the problem is 2 way :

  1. when I implement listview with other controls at the bottom of it I am not able to scroll when listview displays multiple records.
  2. when I use scrollView with and put the listview and other controls inside scrollview , listview displays only one record.

My xml for this is like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="kanix.highrise.com.highrise.generate_material_requisition">

    <!-- TODO: Update blank fragment layout -->

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        >
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/lstTaskQuantity"

        android:layout_weight="1" >


        </ListView>

</LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">
            <TextView
                android:id="@+id/txtLddsdfabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textSize="16dp"
                android:text="Req. From :"
                android:layout_weight="1"
                android:textColor="#fff" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="date"

                android:id="@+id/etDate"
                android:layout_weight=".5"

                android:hint="DD/MM/YYYY"/>

            <ImageButton
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:id="@+id/btnimgcal"


                android:src="@drawable/calender"
                />


        </LinearLayout>



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">
            <TextView
                android:id="@+id/txtLddsdfadsbel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textSize="16dp"
                android:text="For Days :"
                android:layout_weight="1"
                android:textColor="#fff" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="0"

                android:id="@+id/editText"
                android:layout_weight="1.69" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">
            <TextView
                android:id="@+id/txtLddsddfgfadsbel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textSize="16dp"
                android:text="Quantity :"
                android:layout_weight="1"
                android:textColor="#fff" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text="0"
                android:singleLine="true"
                android:width="70dp"

                android:id="@+id/etQuantity"
                android:layout_weight="1.60" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_gravity="center"
            android:orientation="vertical"
            >







            <Button
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#0080FF"
                android:background="#fff"
                android:text="Generate Requisition"
                android:id="@+id/btnSave" />





        </LinearLayout>




</LinearLayout>


    </ScrollView>

</LinearLayout>

Upvotes: 0

Views: 159

Answers (1)

Gopal Singh Sirvi
Gopal Singh Sirvi

Reputation: 4649

You shouldn't put a listview in scrollview if still you want to place then set the height of listview programatically with this method.

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = listView.getPaddingTop()
                + listView.getPaddingBottom();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            if (listItem instanceof ViewGroup) {
                listItem.setLayoutParams(new LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            }
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

Upvotes: 1

Related Questions