2011-05-30 109 views
3

如何將我的列表視圖嵌入到已創建的佈局中?例如,在my_layout.xml我有類似:在另一個視圖中嵌入ListView

<linear layout> 
<textview> 
<*my list view goes here*> 
<linear layout> 

我創建了一個列表的活動,像這樣:

public class HelloListView extends ListActivity { 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES)); 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

     } 
} 

謝謝!

回答

4

你的XML my_layout.xml:

<linear layout> 
<textview> 
     <ListView android:id="@+id/list" 
     android:layout_height="match_parent"     
     android:layout_width="match_parent" 
     > 
     </ListView> 
<linear layout> 

和你的java文件:

public class HelloListView extends Activity { // not use ListActivity 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_layout); 
    ListView lv = (ListView) this.findViewById(R.id.list); 
    // make something for List adapter 
    setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES)); 


    lv.setTextFilterEnabled(true); 

    } 

}

希望能幫助您@monkey

+0

感謝。你的答案看起來是正確的,但是,我得到一個編譯錯誤,它找不到'setListAdapter(...)'。這應該是'lv.setAdapter(...)'?如果是這樣,這是如何實施的?謝謝! – 2011-05-30 03:33:10

相關問題