2012-11-26 46 views
3

我有一個ListView裏面的Linearlayout。這個listview有一個自定義的cursoradapter。 一切工作正常,除了ListView不滾動。ListView不滾動。我如何解決它?

任何建議超過歡迎!謝謝。莫里吉奧

這裏是在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> 

的XML下面是相關片的Java代碼。

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(); } 
     }); 
} 

回答

9

您的ListViews佈局參數是「wrap_content」。當您將新項目添加到litview時它會擴大。因此它不會滾動。如果你將它設置爲「match_parent」,它肯定會開始滾動。並且不要忘記,只有當視圖的內容(孩子查看曾經) 尺寸大於它時纔會滾動。

+0

你是對的,如果我添加它滾動的項目。 –

+0

無關,但讓我在正確的軌道上,我以爲我有很多項目。在列表中,但只有2和是的,它不是滾動..後添加10個項目。我coudl看到它正確滾動。 – reidisaki

3

添加更多超過高度的列表項。那麼你可以滾動。

3

請使用下面的代碼,它會解決你的問題。

<?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>