1

in fact I have the following problem: I have two ListView loaded automatically with two adapters, the problem is in the XML, both list only displays the first element. here is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.hrizi.onescore.home_links.searshscore">

    <include
        android:id="@+id/toolbar"
        layout="@layout/apptoolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:scaleType="fitCenter" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="66dp"
        android:fillViewport="true">

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

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

                <EditText
                    android:id="@+id/sv_search"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_marginLeft="20dp"
                    android:layout_marginRight="20dp"
                    android:background="@drawable/edittextbackground"
                    android:hint="Search ..."
                    android:inputType="text"
                    android:textAlignment="center"></EditText>

                <Button
                    android:id="@+id/btn_search"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginRight="20dp"
                    android:layout_weight="1"
                    android:background="@color/coloronscore"
                    android:onClick="btnsearchOnClick"
                    android:text="Search"
                    android:textAllCaps="false"
                    android:textColor="@color/colorwhite" />
            </LinearLayout>
            <!--Result of research-->
            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="248dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:paddingBottom="5dp" />

            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="248dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:paddingBottom="5dp" />
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

the problem I think in the ScrollView or something else that flushes to that.

thank you in advance .

serg3z
  • 1,852
  • 12
  • 28
hatem dagbouj
  • 139
  • 1
  • 2
  • 12

1 Answers1

0

You can use this function to display all the items in the listview.

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

            int totalHeight = 0;
            int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
                totalHeight += listItem.getMeasuredHeight();
            }

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

Example:

setListViewHeightBasedOnChildren(yourlistview);
Saugat Jonchhen
  • 356
  • 5
  • 16