【问题标题】:ListView of Textview and EditTextTextview 和 EditText 的 ListView
【发布时间】:2014-05-22 13:04:54
【问题描述】:

我对列表的每个元素都有这个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/empty_id"
 >

<TextView
    android:id="@+id/textView_frag_empty_codice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/label_frag_empty_codice"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText_frag_empty_codice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="74dp"
    android:layout_toRightOf="@+id/textView_frag_empty_codice"
    android:ems="10" >

    <requestFocus />
</EditText>
....

如何创建适配器? 现在我收到一个错误:“错误:ArrayAdapter 要求资源 ID 是 TextView” 所以我需要catch这个listview的输入字段,怎么做呢?

【问题讨论】:

  • 但是有可能还是我做不到?
  • 有可能,经常使用tutorial

标签: android android-layout android-intent android-fragments android-listview


【解决方案1】:

ArrayAdapter 要求资源 ID 是 TextView XML 异常意味着您不提供 ArrayAdapter 期望的内容。当您使用此构造函数时: new ArrayAdapter(this, R.layout.a_layout_file, this.file)

R.Layout.a_layout_file 必须是仅包含 TextView 的 xml 布局文件的 id(TextView 不能被其他布局包装,如 LinearLayout、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="wrap_content" 
// other attributes of the TextView
/>

如果你想让你的列表行布局有点不同,那么一个简单的 TextView 小部件使用这个构造函数:

new ArrayAdapter<String>(this, R.layout.a_layout_file, 
R.id.the_id_of_a_textview_from_the_layout, this.file)

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

【讨论】:

    【解决方案2】:

    简单!您不应该将 ArrayAdapter 与组件混合使用,ArrayAdapter 布局应该只包含 TextView Widget。如果您想使用其他 TextView,那么您应该使用 BaseAdapter。

    BaseAdapter 可以混合使用小部件..

    【讨论】:

      【解决方案3】:
      The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:
      
      new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
      R.Layout.a_layout_file must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!), something like this:
      
      <?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
      />
      If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
      
      new ArrayAdapter<String>(this, R.layout.a_layout_file, 
         R.id.the_id_of_a_textview_from_the_layout, this.file)
      
      
      NOTE:In your case you have to use custom adapter.and i think you are using simple adapter.
      
      Thanks
      

      【讨论】: