码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-01 16:15:40      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:如何解决arrayadapter requires the resource id to be a textview

今天碰到这样一个难题,搞了很久都没搞定,到网上搜索了一下,发现有很多人都在问这样的问题,现在将自己解决和分析的结果放置如下:

bubuko.com,布布扣

在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”

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


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

标签:如何解决arrayadapter requires the resource id to be a textview

原文地址:http://623411589.blog.51cto.com/9215003/1584999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!