您好,登錄后才能下訂單哦!
今天碰到這樣一個難題,搞了很久都沒搞定,到網上搜索了一下,發現有很多人都在問這樣的問題,現在將自己解決和分析的結果放置如下:
在ArrayAdapter()這個類中有多個構造方法,我僅據此列以作代表:
ArrayAdapter(Context context ,int textViewResourceId ,List<T> objects)
參數:
context ----> the current context
textViewResourceId ----> the resource ID for a layout file contain a TextView to use when instantiating views.
List<T> objects ---> the objects to represent in the ListView
第一個參數:上下文,就是當前的Activity.
寫法: MainActivity.this this
第二個參數:Android sdk中自己內置的一個布局,它里面只有一個TextView,這個參數是表明我們數組中每一條數據的布局是這個View,就是將每一條數據都顯示在這個View上面,也就是當裝載在這個構造函數中的layout時,其layout的ID 必須是一個TextView,簡言之,第2個參數應該是ListView中每個選擇項的樣式,可以使用系統自帶的 android.R.layout.xxx,也可以是自定義的,僅包含TextView。
創建ArrayAdapter時候必須指定一個resource,該參數決定每個列表項的外觀樣式
simple_list_item_1:
每個列表項都是一個普通的TextView
simple_list_item_2: 有兩個TextView,一個單獨在一行,就是分兩行顯示
simple_list_item_checked: 每個列表項都是一個已勾選的列表項
第三個參數:就是我們要顯示的數據。listView會根據這三個參數,遍歷AdapterData里面的每一條數據,讀出一條,顯示到第二個參數對應的布局中,這樣就形成了我們看到的ListView.
下面是Android sdk自置的布局
路徑:C:\android-sdk_r22.3-windows\android-sdk-windows\platforms\android-16\data\res\layout
simple_list_item_1.xml
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2006 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:paddingLeft="?android:attr/listPreferredItemPaddingLeft" android:paddingRight="?android:attr/listPreferredItemPaddingRight" android:minHeight="?android:attr/listPreferredItemHeightSmall" />
simple_list_item_2.xml
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2006 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?android:attr/listPreferredItemHeight" android:mode="twoLine" > <TextView android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="?android:attr/listPreferredItemPaddingLeft" android:layout_marginTop="8dip" android:textAppearance="?android:attr/textAppearanceListItem" /> <TextView android:id="@android:id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@android:id/text1" android:layout_alignLeft="@android:id/text1" android:textAppearance="?android:attr/textAppearanceSmall" /> </TwoLineListItem>
simple_list_item_checked.xml
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2006 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeightSmall" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:checkMark="?android:attr/textCheckMark" android:paddingLeft="?android:attr/listPreferredItemPaddingLeft" android:paddingRight="?android:attr/listPreferredItemPaddingRight" />
*****************************************************************************************************
因為根節點必須是TextView,不然就會拋“ArrayAdapter requires the resource ID to be a TextView”
*****************************************************************************************************
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。