2014-06-17 80 views
0

嘗試使用自定義遊標適配器將數據從數據庫顯示到列表視圖的問題。我想我已經追蹤到這個問題,無法從xml文件中'找到'我的listview元素。XML文件中的ListView未被拾取

在我的XML文件,我有這樣的:

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="horizontal" 
    android:layout_weight="7" > 

    <ListView 
     android:id="@+id/android:list" 
     android:background="?android:attr/activatedBackgroundIndicator" 
     android:choiceMode="singleChoice" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

我有這個,我對我的活動主要的onCreate:

setContentView(R.layout.activity_trip_display); 
.... // other code 
tripList = (ListView) findViewById(R.id.list); 

但最後一行產生名單不能在Eclipse中解決或不是字段'錯誤。

如果我在那裏添加「機器人」:

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

它編譯OK,但然後我得到的變量「tripList」這會導致其他錯誤NPE空項。

任何想法?

編輯:好吧,我認爲我的問題是我的光標適配器 - 將發佈一個新的問題。謝謝大家的期待...

+0

清理您的項目並重新運行。 – Piyush

回答

1

如果您ListView id爲android:[email protected]:id/list"則無需定義

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

和你Activity必須擴展ListActivity,也無需添加setContentView()@ AndroidWarrior的評論

如果您的ListView ID是android:id="@+id/list"那麼初始化您的ListView

tripList = (ListView) findViewById(R.id.list); 

到這個更多的演示http://www.vogella.com/tutorials/AndroidListView/article.html

+0

@simple計劃如果他'Activity'延伸'ListActivity'那麼他可能不會需要一個XML可言,除了那個'的setContentView()'也不會被要求 – CodeWarrior

0

代替android:id="@+id/android:list"使用android:id="@+id/list"

+0

試過了 - 應用程序崩潰與「您的內容必須有一個ListView的ID屬性是'android.R.id.list' – Nelmo

+0

你試過乾淨的皮棉標記嗎? – Martynas

0

如果你想在你的編碼

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

那麼你的類必須與ListActivity延伸到使用這個。

,並在你的XML改變爲ListView ID喜歡

android:id="@+id/android:list" 

android:id ="@android:id/list" 
+0

我擴展ListActivity - 如果我使用android.R.id.list,會發生什麼是我的listView確實被創建,但我的自定義遊標適配器中的bindView沒有被調用,所以沒有數據顯示。我想我會根據這些原則發佈一個新問題。 – Nelmo

0

我覺得跟Android的問題:ID = 「@ + ID /安卓列表」代之以此

android:id =「@ + id/list」並將您的應用程序擴展爲Activity 足夠。

ListView list =(ListView)findViewById(R.id。清單);

接受ANS如果問題解決了。

+0

試過了 - 與應用程序崩潰「你的內容必須有一個ListView其id屬性爲‘android.R.id.list’ – Nelmo

+0

哦然後儘量延長ListActivity @Nelmo – Android

+0

已經這樣做了...... – Nelmo