Android ArrayAdapter要求ID为TextView错误

Android ArrayAdapter要求ID为TextView错误,android,listview,textview,android-arrayadapter,android-relativelayout,Android,Listview,Textview,Android Arrayadapter,Android Relativelayout,我试图为我的列表项创建一个好的布局,但我的代码只有在简化如下时才能工作: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddi

我试图为我的列表项创建一个好的布局,但我的代码只有在简化如下时才能工作:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    // other attributes of the TextView
/>


构建您自己的ArrayAdapter,然后您可以按照自己的意愿进行布局。

您可能正在使用类似以下内容():

ArrayAdapter adapter=新的ArrayAdapter(这个,R.layout.layout_1,值);
在这种情况下,布局必须是带有文本视图的简单布局


如果要使用自己的布局,则需要编写自定义适配器。

ArrayAdapter要求资源ID为TextView XML异常,这意味着您无法提供ArrayAdapter所需的内容。使用此构造函数时:

new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
new ArrayAdapter<String>(this, R.layout.a_layout_file, 
   R.id.the_id_of_a_textview_from_the_layout, this.file)
如果您希望列表行布局有所不同,那么简单的TextView小部件可以使用以下构造函数:

new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
new ArrayAdapter<String>(this, R.layout.a_layout_file, 
   R.id.the_id_of_a_textview_from_the_layout, this.file)
new ArrayAdapter(此,R.layout.a_layout_文件,
R.id.a\u文本视图的\u id\u来自\u布局,this.file)

其中,您提供的布局id可以包含各种视图,但还必须包含传递给ArrayAdapter的带有和id(第三个参数)的TextView,以便ArrayAdapter知道在行布局中放置字符串的位置。

您似乎在正确的位置。我不确定你的代码的确切问题,因为我没有仔细比较过,但它确实有效

完整教程如下所示:


这避免了错误
ArrayAdapter要求ID是我也得到的TextView

此外,ArrayAdapter还有另一个构造函数,它将任何资源作为输入,您可以指定用于填充的TextView ID,int,int,T[]).你能告诉我一个好的教程,教我如何创建自己的listAdapter吗?开发者正在解释listview是如何工作的,以及如何使其正常工作。感谢视频的帮助,但现在我无法直接访问我的信息以显示在listview中。请按照下面链接的第一个答案[[1]:如果我的ArrayAdapter使用的单元格是自定义布局,并且单元格中有许多文本视图,该怎么办?这不会有问题。您可以在ArrayAdapter中使用多个视图,而不是单个按钮或文本视图。@NinjaCoder
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    // other attributes of the TextView
/>
new ArrayAdapter<String>(this, R.layout.a_layout_file, 
   R.id.the_id_of_a_textview_from_the_layout, this.file)