2013-03-03 34 views
0

我正在嘗試將MapViewListView結合使用。我的理解是,我需要擴展MapActivity,然後獲取要在我的課程中使用的ListView的引用。我的問題是我無法獲得對ListView的參考。Android:如何獲得對listView的引用?

這是我的XML:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/AbsoluteLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <com.google.android.maps.MapView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/mapview" 
     android:layout_width="match_parent" 
     android:layout_height="150dp" 
     android:layout_y="0dp" 
     android:clickable="true" /> 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="268dp" 
     android:layout_y="151dp" > 

    </ListView> 

</AbsoluteLayout> 

這是我想獲得的ListView參考:

//instance vars 
ListView listView; 

//get ref to listview 
listView = findViewById(R.id.list); 

出於某種原因,它說,名單是不是現場。我是否錯誤地引用了它?

任何幫助表示讚賞。

+1

'AbsoluteLayout'已被棄用:) – Geros 2013-03-03 00:09:26

回答

0

應該

listView = (ListView) findViewById(android.R.id.list); 

但是,因爲你沒有使用ListActivity,你不"@android:id/list""@+id/list"(或任何自定義)將工作。

通常情況下,您在需要時使用Android ID,例如使用ListActivity/ListFragment時只能在您的自定義佈局包含正確的ID時才能使用。

此外,您應該使用MapFragments/SupportMapFragment s代替MapActivity is deprecated