The ListView does not scroll. How do I fix it?

13,651

Solution 1

Your ListViews Layout param is "wrap_content". it will expand as you add new items to litview. therefore it won't scroll. If you set it to "match_parent" it will sure start scrolling.And dont forget that a view does scroll only if its contents(childs view what ever) size is bigger than it.

Solution 2

Add more list items that exceeds height. then you can scroll.

Solution 3

Please use below code, it will solve your problem.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Aggiungi" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_below="@+id/buttonAdd"/>

</RelativeLayout>
Share:
13,651
Lisa Anne
Author by

Lisa Anne

Updated on June 05, 2022

Comments

  • Lisa Anne
    Lisa Anne almost 2 years

    I have a ListView inside a Linearlayout. The listview has a custom cursoradapter. Everything works fine, except the ListView does not scroll.

    Any suggestion more than welcome!! Thanks. maurizio

    Here is the XML of the MainActivity

    <?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" >
    
        <Button
            android:id="@+id/buttonAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="Aggiungi" />
    
        <ListView
            android:id="@+id/list"  
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
    </LinearLayout>
    

    Here is the relevant piece of java code.

    public class MainActivity extends Activity  {
        private DatabaseHelper db=null;
        private Cursor tabellaCursor=null;
        private ListAdapter adapter; 
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            int[] to = new int[] {R.id.name_entry};
            db = new DatabaseHelper(this);
            tabellaCursor=db.getWritableDatabase().rawQuery("SELECT _id, colonna1, colo      nna2, colonna3 FROM tabella ORDER BY _id", null);
            ListAdapter adapter=new MyAdapter(this, R.layout.list_example_entry, tabe     llaCursor, new String[]{"colonna1"},to);
            ListView lt = (ListView)findViewById(R.id.list); 
            lt.setAdapter(adapter); 
            Button addbtn=(Button)findViewById(R.id.buttonAdd);
            addbtn.setOnClickListener(new OnClickListener()
            {public void onClick(View v){add(); }
            });
    }
    
  • reidisaki
    reidisaki about 9 years
    unrelated but got me on the right track, i thought i had a lot of items. in the list but there were only 2 and yes, it wasn't scrolling.. after adding 10 more items. i coudl see it scroll correctly.