溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何解決ArrayAdapter requires the resource ID to be a TextView

發(fā)布時間:2020-07-05 07:15:30 來源:網(wǎng)絡(luò) 閱讀:1649 作者:王穎1 欄目:移動開發(fā)

今天碰到這樣一個難題,搞了很久都沒搞定,到網(wǎng)上搜索了一下,發(fā)現(xiàn)有很多人都在問這樣的問題,現(xiàn)在將自己解決和分析的結(jié)果放置如下:

如何解決ArrayAdapter requires the resource ID to be a TextView

在ArrayAdapter()這個類中有多個構(gòu)造方法,我僅據(jù)此列以作代表:

ArrayAdapter(Context context ,int textViewResourceId ,List<T> objects)

參數(shù):

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


第一個參數(shù):上下文,就是當前的Activity.

          寫法: MainActivity.this      this


第二個參數(shù):Android sdk中自己內(nèi)置的一個布局,它里面只有一個TextView,這個參數(shù)是表明我們數(shù)組中每一條數(shù)據(jù)的布局是這個View,就是將每一條數(shù)據(jù)都顯示在這個View上面,也就是當裝載在這個構(gòu)造函數(shù)中的layout時,其layout的ID 必須是一個TextView,簡言之,第2個參數(shù)應(yīng)該是ListView中每個選擇項的樣式,可以使用系統(tǒng)自帶的 android.R.layout.xxx,也可以是自定義的,僅包含TextView。

                          

創(chuàng)建ArrayAdapter時候必須指定一個resource,該參數(shù)決定每個列表項的外觀樣式

simple_list_item_1: 每個列表項都是一個普通的TextView

simple_list_item_2有兩個TextView,一個單獨在一行,就是分兩行顯示

simple_list_item_checked: 每個列表項都是一個已勾選的列表項

      

第三個參數(shù):就是我們要顯示的數(shù)據(jù)。listView會根據(jù)這三個參數(shù),遍歷AdapterData里面的每一條數(shù)據(jù),讀出一條,顯示到第二個參數(shù)對應(yīng)的布局中,這樣就形成了我們看到的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"
/>


*****************************************************************************************************

因為根節(jié)點必須是TextView,不然就會拋“ArrayAdapter requires the resource ID to be a TextView”

*****************************************************************************************************


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI